diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml new file mode 100644 index 0000000..98a929e --- /dev/null +++ b/.github/workflows/checks.yml @@ -0,0 +1,17 @@ +name: Checks + +on: [push] + +jobs: + test: + name: Test codebase + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: go.mod + cache: true + + - run: make tests \ No newline at end of file diff --git a/internal/api/server.go b/internal/api/server.go index fe3be8e..118b574 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -142,7 +142,6 @@ func (s *Server) GetQRCodeFromStore(ctx context.Context, request GetQRCodeFromSt // QRStore - store QR code func (s *Server) QRStore(ctx context.Context, request QRStoreRequestObject) (QRStoreResponseObject, error) { - // generate random key uv, err := uuid.NewV4() if err != nil { return QRStore500JSONResponse{ @@ -152,10 +151,8 @@ func (s *Server) QRStore(ctx context.Context, request QRStoreRequestObject) (QRS }, nil } - // store data in map s.cache.Set(uv.String(), request.Body, 1*time.Hour) hostURL := s.cfg.Host - // write key to response shortURL := fmt.Sprintf("%s%s?id=%s", hostURL, "/qr-store", uv.String()) return QRStore200JSONResponse(shortURL), nil } diff --git a/internal/api/server_test.go b/internal/api/server_test.go index 3222cf5..2103826 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/gofrs/uuid/v5" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -367,6 +368,89 @@ func TestSignIn(t *testing.T) { } } +func TestQRStore(t *testing.T) { + ctx := context.Background() + server := New(cfg, keysLoader) + + type bodyType struct { + CallbackUrl *string `json:"callbackUrl,omitempty"` + Reason *string `json:"reason,omitempty"` + Scope *[]Scope `json:"scope,omitempty"` + } + + type expected struct { + httpCode int + QRStoreResponseObject + } + + type testConfig struct { + name string + body QRStoreRequestObject + expected expected + } + + for _, tc := range []testConfig{ + { + name: "valid request", + body: QRStoreRequestObject{ + Body: &QRStoreJSONRequestBody{ + From: "", + To: common.ToPointer(""), + Typ: "", + Type: "", + Body: bodyType{ + CallbackUrl: common.ToPointer("http://localhost:3000/callback?n=1"), + Reason: common.ToPointer("reason"), + Scope: &[]Scope{ + { + CircuitId: "credentialAtomicQuerySigV2", + Id: 1, + Query: map[string]interface{}{}, + }, + }, + }, + }, + }, + expected: expected{ + httpCode: http.StatusOK, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + rr, err := server.QRStore(ctx, tc.body) + require.NoError(t, err) + switch tc.expected.httpCode { + case http.StatusOK: + response, ok := rr.(QRStore200JSONResponse) + require.True(t, ok) + assert.True(t, isValidaQrStoreCallback(t, string(response))) + default: + t.Errorf("unexpected http code: %d", tc.expected.httpCode) + } + }) + } +} + +func isValidaQrStoreCallback(t *testing.T, url string) bool { + callBackURL := url + items := strings.Split(callBackURL, "?") + if len(items) != 2 { + return false + } + if items[0] != cfg.Host+"/qr-store" { + return false + } + + queryItems := strings.Split(items[1], "=") + if len(queryItems) != 2 { + return false + } + + _, err := uuid.FromString(queryItems[1]) + require.NoError(t, err) + return true +} + func isValidCallBack(t *testing.T, url *string) bool { callBackURL := *url items := strings.Split(callBackURL, "?")