Skip to content

Commit

Permalink
feat: add more tests and gh action
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsaporiti committed Dec 19, 2023
1 parent 7d8363f commit a6cae16
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
Expand Down
84 changes: 84 additions & 0 deletions internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/gofrs/uuid/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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, "?")
Expand Down

0 comments on commit a6cae16

Please sign in to comment.