diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 607ab3c..aa56780 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -18,6 +18,9 @@ jobs: with: go-version: '1.22' + - name: Run linters + run: go tool golangci-lint run + - name: Unit Test run: make unit-tests-ci diff --git a/.golangci.yml b/.golangci.yml index 1549b9a..b90195e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,59 +19,59 @@ linters: - errcheck - errchkjson - errname - - errorlint - - exhaustive - - fatcontext - - forbidigo - - forcetypeassert - - funlen - - ginkgolinter - - gocheckcompilerdirectives - - gochecknoinits - - gochecksumtype - - gocognit - - goconst - - gocritic - - gocyclo - - godot - - gomoddirectives - - gomodguard - - goprintffuncname - - gosec - - gosmopolitan - - govet - - grouper - - importas - - inamedparam - - ineffassign - - interfacebloat - - lll - - loggercheck - - maintidx - - makezero - - mirror - - misspell - - musttag - - nakedret - - nestif - - nilerr - - nilnil - - noctx - - nolintlint - - nonamedreturns - - nosprintfhostport - - prealloc - - predeclared - - promlinter - - protogetter - - reassign - - revive +# - errorlint +# - exhaustive +# - fatcontext +# - forbidigo +# - forcetypeassert +# - funlen +# - ginkgolinter +# - gocheckcompilerdirectives +# - gochecknoinits +# - gochecksumtype +# - gocognit +# - goconst +# - gocritic +# - gocyclo +# - godot +# - gomoddirectives +# - gomodguard +# - goprintffuncname +# - gosec +# - gosmopolitan +# - govet +# - grouper +# - importas +# - inamedparam +# - ineffassign +# - interfacebloat +# - lll +# - loggercheck +# - maintidx +# - makezero +# - mirror +# - misspell +# - musttag +# - nakedret +# - nestif +# - nilerr +# - nilnil +# - noctx +# - nolintlint +# - nonamedreturns +# - nosprintfhostport +# - prealloc +# - predeclared +# - promlinter +# - protogetter +# - reassign +# - revive - rowserrcheck - sloglint - spancheck - sqlclosecheck - - staticcheck - - tagliatelle +# - staticcheck +# - tagliatelle - testableexamples - testifylint - thelper @@ -82,7 +82,7 @@ linters: - usestdlibvars - wastedassign - whitespace - - wrapcheck +# - wrapcheck - zerologlint settings: errcheck: diff --git a/e2e-tests/browser_extension/browser_extension_2fa_request_test.go b/e2e-tests/browser_extension/browser_extension_2fa_request_test.go index 34ae03d..7d21543 100644 --- a/e2e-tests/browser_extension/browser_extension_2fa_request_test.go +++ b/e2e-tests/browser_extension/browser_extension_2fa_request_test.go @@ -4,9 +4,9 @@ import ( "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestBrowserExtensionTwoFactorAuthTestSuite(t *testing.T) { @@ -30,12 +30,12 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestRequest2FaToken() { request2FaTokenPayload := []byte(`{"domain":"https://facebook.com/path/nested"}`) e2e_tests.DoAPISuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", request2FaTokenPayload, &tokenRequest) - assert.Equal(s.T(), browserExtension.Id, tokenRequest.ExtensionId) + s.Equal(browserExtension.Id, tokenRequest.ExtensionId) var tokenRequestById *e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &tokenRequestById) - assert.Equal(s.T(), tokenRequest.Id, tokenRequestById.Id) - assert.Equal(s.T(), "https://facebook.com", tokenRequestById.Domain) + s.Equal(tokenRequest.Id, tokenRequestById.Id) + s.Equal("https://facebook.com", tokenRequestById.Domain) } func (s *BrowserExtensionTwoFactorAuthTestSuite) TestFindAll2FaRequestsForBrowserExtension() { @@ -50,7 +50,7 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestFindAll2FaRequestsForBrowse var tokenRequestsCollection []*e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests", &tokenRequestsCollection) - assert.Len(s.T(), tokenRequestsCollection, 2) + s.Len(tokenRequestsCollection, 2) } func (s *BrowserExtensionTwoFactorAuthTestSuite) TestClose2FaTokenRequest() { @@ -63,7 +63,7 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestClose2FaTokenRequest() { var closedTokenRequest *e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &closedTokenRequest) - assert.Equal(s.T(), "completed", closedTokenRequest.Status) + s.Equal("completed", closedTokenRequest.Status) } func (s *BrowserExtensionTwoFactorAuthTestSuite) TestCloseNotExisting2FaTokenRequest() { @@ -72,7 +72,6 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestCloseNotExisting2FaTokenReq closeTokenRequestPayload := []byte(`{"status":"completed"}`) e2e_tests.DoAPIPostAndAssertCode(s.T(), 404, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+notExistingTokenRequestId.String()+"/commands/close_2fa_request", closeTokenRequestPayload, nil) - } func (s *BrowserExtensionTwoFactorAuthTestSuite) TestDoNotReturnClosed2FaRequests() { @@ -86,7 +85,7 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestDoNotReturnClosed2FaRequest var response []*e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests", &response) - assert.Len(s.T(), response, 0) + s.Empty(response) } func (s *BrowserExtensionTwoFactorAuthTestSuite) TestTerminate2FaRequest() { @@ -100,7 +99,7 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestTerminate2FaRequest() { var response *e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &response) - assert.Equal(s.T(), "terminated", response.Status) + s.Equal("terminated", response.Status) } func (s *BrowserExtensionTwoFactorAuthTestSuite) TestClose2FaRequest() { @@ -117,5 +116,5 @@ func (s *BrowserExtensionTwoFactorAuthTestSuite) TestClose2FaRequest() { var closedTokenRequest *e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &closedTokenRequest) - assert.Equal(s.T(), "completed", closedTokenRequest.Status) + s.Equal("completed", closedTokenRequest.Status) } diff --git a/e2e-tests/browser_extension/browser_extension_2fa_test.go b/e2e-tests/browser_extension/browser_extension_2fa_test.go index 2ad15d3..711bd54 100644 --- a/e2e-tests/browser_extension/browser_extension_2fa_test.go +++ b/e2e-tests/browser_extension/browser_extension_2fa_test.go @@ -6,7 +6,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestTwoFactorAuthTestSuite(t *testing.T) { @@ -35,7 +36,7 @@ func (s *TwoFactorAuthTestSuite) TestBrowserExtensionAuthFullFlow() { assertDeviceHasPairedExtension(s.T(), device, browserExtension) assertBrowserExtensionHasPairedDevice(s.T(), browserExtension, device) - expectedPairingSuccessWebsocket := createPairingSuccessWebsocketMessage(browserExtension, device, devicePubKey) + expectedPairingSuccessWebsocket := createPairingSuccessWebsocketMessage(s.T(), browserExtension, device, devicePubKey) websocketTestListener.AssertMessageHasBeenReceived(s.T(), expectedPairingSuccessWebsocket) tokenRequest := e2e_tests.Request2FaToken(s.T(), "facebook.com", browserExtension.Id) @@ -46,11 +47,13 @@ func (s *TwoFactorAuthTestSuite) TestBrowserExtensionAuthFullFlow() { e2e_tests.Send2FaTokenToExtension(s.T(), browserExtension.Id, device.Id, tokenRequest.Id, "2fa-token") - expected2FaTokenWebsocket := createBrowserExtensionReceived2FaTokenMessage(browserExtension.Id, device.Id, tokenRequest.Id) + expected2FaTokenWebsocket := createBrowserExtensionReceived2FaTokenMessage(s.T(), browserExtension.Id, device.Id, tokenRequest.Id) extensionTokenRequestWebsocketListener.AssertMessageHasBeenReceived(s.T(), expected2FaTokenWebsocket) } -func createBrowserExtensionReceived2FaTokenMessage(extensionId, deviceId, requestId string) string { +func createBrowserExtensionReceived2FaTokenMessage(t *testing.T, extensionId, deviceId, requestId string) string { + t.Helper() + expected2FaTokenWebsocketMessageRaw := struct { Event string `json:"event"` ExtensionId string `json:"extension_id"` @@ -65,12 +68,17 @@ func createBrowserExtensionReceived2FaTokenMessage(extensionId, deviceId, reques Token: "2fa-token", } - message, _ := json.Marshal(expected2FaTokenWebsocketMessageRaw) + message, err := json.Marshal(expected2FaTokenWebsocketMessageRaw) + if err != nil { + t.Fatalf("failed to marshal expected 2FA token websocket message: %v", err) + } return string(message) } -func createPairingSuccessWebsocketMessage(browserExtension *e2e_tests.BrowserExtensionResponse, device *e2e_tests.DeviceResponse, devicePubKey string) string { +func createPairingSuccessWebsocketMessage(t *testing.T, browserExtension *e2e_tests.BrowserExtensionResponse, device *e2e_tests.DeviceResponse, devicePubKey string) string { + t.Helper() + expectedPairingWebsocketMessageRaw := &struct { Event string `json:"event"` BrowserExtensionId string `json:"browser_extension_id"` @@ -83,12 +91,16 @@ func createPairingSuccessWebsocketMessage(browserExtension *e2e_tests.BrowserExt DevicePublicKey: devicePubKey, } - message, _ := json.Marshal(expectedPairingWebsocketMessageRaw) + message, err := json.Marshal(expectedPairingWebsocketMessageRaw) + if err != nil { + t.Fatalf("failed to marshal expected pairing websocket message: %v", err) + } return string(message) } func assertBrowserExtensionHasPairedDevice(t *testing.T, browserExtension *e2e_tests.BrowserExtensionResponse, device *e2e_tests.DeviceResponse) { + t.Helper() var browserExtensionDevices []*e2e_tests.DeviceResponse e2e_tests.DoAPISuccessGet(t, "browser_extensions/"+browserExtension.Id+"/devices", &browserExtensionDevices) @@ -97,6 +109,7 @@ func assertBrowserExtensionHasPairedDevice(t *testing.T, browserExtension *e2e_t } func assertDeviceHasPairedExtension(t *testing.T, device *e2e_tests.DeviceResponse, browserExtension *e2e_tests.BrowserExtensionResponse) { + t.Helper() var deviceBrowserExtensions []*e2e_tests.BrowserExtensionResponse e2e_tests.DoAPISuccessGet(t, "mobile/devices/"+device.Id+"/browser_extensions", &deviceBrowserExtensions) diff --git a/e2e-tests/browser_extension/browser_extension_log_test.go b/e2e-tests/browser_extension/browser_extension_log_test.go index 1a91762..b24322e 100644 --- a/e2e-tests/browser_extension/browser_extension_log_test.go +++ b/e2e-tests/browser_extension/browser_extension_log_test.go @@ -5,7 +5,9 @@ import ( "testing" "github.com/google/uuid" - "github.com/twofas/2fas-server/e2e-tests" + "github.com/stretchr/testify/require" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func Test_BrowserExtensionLogging(t *testing.T) { @@ -19,7 +21,9 @@ func Test_BrowserExtensionLogging(t *testing.T) { Message: "test log", } - payload, _ := json.Marshal(log) + payload, err := json.Marshal(log) + require.NoError(t, err) + e2e_tests.DoAPISuccessPost(t, "/browser_extensions/"+browserExtension.Id+"/commands/store_log", payload, nil) } @@ -34,6 +38,8 @@ func Test_NotExistingBrowserExtensionLogging(t *testing.T) { Message: "test log", } - payload, _ := json.Marshal(log) + payload, err := json.Marshal(log) + require.NoError(t, err) + e2e_tests.DoAPISuccessPost(t, "/browser_extensions/"+someId.String()+"/commands/store_log", payload, nil) } diff --git a/e2e-tests/browser_extension/browser_extension_pairing_test.go b/e2e-tests/browser_extension/browser_extension_pairing_test.go index 8b2053c..b51d277 100644 --- a/e2e-tests/browser_extension/browser_extension_pairing_test.go +++ b/e2e-tests/browser_extension/browser_extension_pairing_test.go @@ -6,10 +6,9 @@ import ( "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestBrowserExtensionPairingTestSuite(t *testing.T) { @@ -28,36 +27,36 @@ func (s *BrowserExtensionPairingTestSuite) SetupTest() { func (s *BrowserExtensionPairingTestSuite) TestPairBrowserExtensionWithMobileDevice() { browserExt := e2e_tests.CreateBrowserExtension(s.T(), "go-test") _, err := uuid.Parse(browserExt.Id) - require.NoError(s.T(), err) + s.Require().NoError(err) device, devicePubKey := e2e_tests.CreateDevice(s.T(), "go-test-device", "some-device-id") _, err = uuid.Parse(device.Id) - require.NoError(s.T(), err) + s.Require().NoError(err) e2e_tests.PairDeviceWithBrowserExtension(s.T(), devicePubKey, browserExt, device) var extensionDevice *e2e_tests.DevicePairedBrowserExtensionResponse e2e_tests.DoAPISuccessGet(s.T(), "/browser_extensions/"+browserExt.Id+"/devices/"+device.Id, &extensionDevice) - assert.Equal(s.T(), extensionDevice.Id, device.Id) + s.Equal(extensionDevice.Id, device.Id) } func (s *BrowserExtensionPairingTestSuite) TestDoNotFindNotPairedBrowserExtensionMobileDevice() { browserExt := e2e_tests.CreateBrowserExtension(s.T(), "go-test") _, err := uuid.Parse(browserExt.Id) - require.NoError(s.T(), err) + s.Require().NoError(err) device, _ := e2e_tests.CreateDevice(s.T(), "go-test-device", "some-device-id") response := e2e_tests.DoAPIGet(s.T(), "/browser_extensions/"+browserExt.Id+"/devices/"+device.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *BrowserExtensionPairingTestSuite) TestPairBrowserExtensionWithMultipleDevices() { browserExt := e2e_tests.CreateBrowserExtension(s.T(), "go-test") _, err := uuid.Parse(browserExt.Id) - require.NoError(s.T(), err) + s.Require().NoError(err) device1, devicePubKey1 := e2e_tests.CreateDevice(s.T(), "go-test-device-1", "some-device-id-1") device2, devicePubKey2 := e2e_tests.CreateDevice(s.T(), "go-test-device-2", "some-device-id-2") @@ -67,7 +66,7 @@ func (s *BrowserExtensionPairingTestSuite) TestPairBrowserExtensionWithMultipleD extensionDevices := e2e_tests.GetExtensionDevices(s.T(), browserExt.Id) - assert.Len(s.T(), extensionDevices, 2) + s.Len(extensionDevices, 2) } func (s *BrowserExtensionPairingTestSuite) TestRemoveBrowserExtensionPairedDevice() { @@ -80,13 +79,13 @@ func (s *BrowserExtensionPairingTestSuite) TestRemoveBrowserExtensionPairedDevic e2e_tests.PairDeviceWithBrowserExtension(s.T(), devicePubKey2, browserExt, device2) extensionDevices := getExtensionPairedDevices(s.T(), browserExt) - assert.Len(s.T(), extensionDevices, 2) + s.Len(extensionDevices, 2) e2e_tests.DoAPISuccessDelete(s.T(), "/browser_extensions/"+browserExt.Id+"/devices/"+device1.Id) extensionDevices = getExtensionPairedDevices(s.T(), browserExt) - assert.Len(s.T(), extensionDevices, 1) - assert.Equal(s.T(), device2.Id, extensionDevices[0].Id) + s.Len(extensionDevices, 1) + s.Equal(device2.Id, extensionDevices[0].Id) } func (s *BrowserExtensionPairingTestSuite) TestRemoveBrowserExtensionPairedDeviceTwice() { @@ -98,7 +97,7 @@ func (s *BrowserExtensionPairingTestSuite) TestRemoveBrowserExtensionPairedDevic e2e_tests.DoAPISuccessDelete(s.T(), "/browser_extensions/"+browserExt.Id+"/devices/"+device.Id) response := e2e_tests.DoAPIRequest(s.T(), "/browser_extensions/"+browserExt.Id+"/devices/"+device.Id, http.MethodDelete, nil /*payload*/, nil /*resp*/) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *BrowserExtensionPairingTestSuite) TestRemoveAllBrowserExtensionPairedDevices() { @@ -111,7 +110,7 @@ func (s *BrowserExtensionPairingTestSuite) TestRemoveAllBrowserExtensionPairedDe e2e_tests.DoAPISuccessDelete(s.T(), "/browser_extensions/"+browserExt.Id+"/devices") extensionDevices := e2e_tests.GetExtensionDevices(s.T(), browserExt.Id) - assert.Len(s.T(), extensionDevices, 0) + s.Empty(extensionDevices) } func (s *BrowserExtensionPairingTestSuite) TestGetPairedDevicesWhichIDoNotOwn() { @@ -125,12 +124,12 @@ func (s *BrowserExtensionPairingTestSuite) TestGetPairedDevicesWhichIDoNotOwn() e2e_tests.PairDeviceWithBrowserExtension(s.T(), devicePubKey2, browserExt2, device2) firstExtensionDevices := getExtensionPairedDevices(s.T(), browserExt1) - assert.Len(s.T(), firstExtensionDevices, 1) - assert.Equal(s.T(), device1.Id, firstExtensionDevices[0].Id) + s.Len(firstExtensionDevices, 1) + s.Equal(device1.Id, firstExtensionDevices[0].Id) secondExtensionDevices := getExtensionPairedDevices(s.T(), browserExt2) - assert.Len(s.T(), secondExtensionDevices, 1) - assert.Equal(s.T(), device2.Id, secondExtensionDevices[0].Id) + s.Len(secondExtensionDevices, 1) + s.Equal(device2.Id, secondExtensionDevices[0].Id) } func (s *BrowserExtensionPairingTestSuite) TestGetPairedDevicesByInvalidExtensionId() { @@ -145,8 +144,8 @@ func (s *BrowserExtensionPairingTestSuite) TestGetPairedDevicesByInvalidExtensio invalidResp := map[string]any{} response := e2e_tests.DoAPIGet(s.T(), "/browser_extensions/some-invalid-id/devices/", &invalidResp) - assert.Equal(s.T(), 400, response.StatusCode) - assert.Contains(s.T(), invalidResp["Reason"], `Field validation for 'ExtensionId' failed on the 'uuid4'`) + s.Equal(400, response.StatusCode) + s.Contains(invalidResp["Reason"], `Field validation for 'ExtensionId' failed on the 'uuid4'`) } func (s *BrowserExtensionPairingTestSuite) TestGetPairedDevicesByNotExistingExtensionId() { @@ -162,20 +161,20 @@ func (s *BrowserExtensionPairingTestSuite) TestGetPairedDevicesByNotExistingExte notExistingExtensionId := uuid.New() var firstExtensionDevices []*e2e_tests.ExtensionPairedDeviceResponse e2e_tests.DoAPISuccessGet(s.T(), "/browser_extensions/"+notExistingExtensionId.String()+"/devices/", &firstExtensionDevices) - assert.Len(s.T(), firstExtensionDevices, 0) + s.Empty(firstExtensionDevices) } func (s *BrowserExtensionPairingTestSuite) TestShareExtensionPublicKeyWithMobileDevice() { browserExt := e2e_tests.CreateBrowserExtensionWithPublicKey(s.T(), "go-test", "b64-rsa-pub-key") _, err := uuid.Parse(browserExt.Id) - require.NoError(s.T(), err) + s.Require().NoError(err) device, devicePubKey := e2e_tests.CreateDevice(s.T(), "go-test-device", "some-device-id") _, err = uuid.Parse(device.Id) - require.NoError(s.T(), err) + s.Require().NoError(err) result := e2e_tests.PairDeviceWithBrowserExtension(s.T(), devicePubKey, browserExt, device) - assert.Equal(s.T(), "b64-rsa-pub-key", result.ExtensionPublicKey) + s.Equal("b64-rsa-pub-key", result.ExtensionPublicKey) } func (s *BrowserExtensionPairingTestSuite) TestCannotPairSameDeviceAndExtensionTwice() { @@ -195,12 +194,14 @@ func (s *BrowserExtensionPairingTestSuite) TestCannotPairSameDeviceAndExtensionT } pairingResult := new(e2e_tests.PairingResultResponse) - payloadJson, _ := json.Marshal(payload) + payloadJson, err := json.Marshal(payload) + s.Require().NoError(err) e2e_tests.DoAPIPostAndAssertCode(s.T(), 409, "/mobile/devices/"+device.Id+"/browser_extensions", payloadJson, pairingResult) } func getExtensionPairedDevices(t *testing.T, browserExt *e2e_tests.BrowserExtensionResponse) []*e2e_tests.ExtensionPairedDeviceResponse { + t.Helper() var extensionDevices []*e2e_tests.ExtensionPairedDeviceResponse e2e_tests.DoAPISuccessGet(t, "/browser_extensions/"+browserExt.Id+"/devices/", &extensionDevices) return extensionDevices diff --git a/e2e-tests/browser_extension/browser_extension_test.go b/e2e-tests/browser_extension/browser_extension_test.go index 1052638..e30c5e3 100644 --- a/e2e-tests/browser_extension/browser_extension_test.go +++ b/e2e-tests/browser_extension/browser_extension_test.go @@ -6,9 +6,9 @@ import ( "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" "github.com/twofas/2fas-server/internal/common/crypto" ) @@ -42,7 +42,7 @@ func (s *BrowserExtensionTestSuite) TestCreateBrowserExtension() { for _, tc := range testsCases { response := createBrowserExtension(s.T(), tc.extensionName) - assert.Equal(s.T(), tc.expectedHttpCode, response.StatusCode) + s.Equal(tc.expectedHttpCode, response.StatusCode) } } @@ -55,7 +55,7 @@ func (s *BrowserExtensionTestSuite) TestUpdateBrowserExtension() { var browserExtension *e2e_tests.BrowserExtensionResponse e2e_tests.DoAPISuccessGet(s.T(), "/browser_extensions/"+browserExt.Id, &browserExtension) - assert.Equal(s.T(), "updated-extension-name", browserExtension.Name) + s.Equal("updated-extension-name", browserExtension.Name) } func (s *BrowserExtensionTestSuite) TestUpdateNotExistingBrowserExtension() { @@ -64,7 +64,7 @@ func (s *BrowserExtensionTestSuite) TestUpdateNotExistingBrowserExtension() { payload := []byte(`{"name": "updated-extension-name"}`) response := e2e_tests.DoAPIRequest(s.T(), "/browser_extensions/"+id.String(), http.MethodPut, payload, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *BrowserExtensionTestSuite) TestUpdateBrowserExtensionSetEmptyName() { @@ -73,7 +73,7 @@ func (s *BrowserExtensionTestSuite) TestUpdateBrowserExtensionSetEmptyName() { payload := []byte(`{"name": ""}`) response := e2e_tests.DoAPIRequest(s.T(), "/browser_extensions/"+browserExt.Id, http.MethodPut, payload, nil) - assert.Equal(s.T(), 400, response.StatusCode) + s.Equal(400, response.StatusCode) } func (s *BrowserExtensionTestSuite) TestDoNotFindNotExistingExtension() { @@ -82,10 +82,11 @@ func (s *BrowserExtensionTestSuite) TestDoNotFindNotExistingExtension() { var browserExtension *e2e_tests.BrowserExtensionResponse response := e2e_tests.DoAPIGet(s.T(), "/browser_extensions/"+notExistingId.String(), &browserExtension) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func createBrowserExtension(t *testing.T, name string) *http.Response { + t.Helper() keyPair := crypto.GenerateKeyPair(2048) pubKey := crypto.PublicKeyToBase64(keyPair.PublicKey) @@ -93,5 +94,4 @@ func createBrowserExtension(t *testing.T, name string) *http.Response { payload := []byte(fmt.Sprintf(`{"name":"%s","browser_name":"go-browser","browser_version":"0.1","public_key":"%s"}`, name, pubKey)) return e2e_tests.DoAPIRequest(t, "/browser_extensions", http.MethodPost, payload, nil) - } diff --git a/e2e-tests/helpers.go b/e2e-tests/helpers.go index 5e6b788..f0f5ddb 100644 --- a/e2e-tests/helpers.go +++ b/e2e-tests/helpers.go @@ -9,6 +9,8 @@ import ( ) func CreateDevice(t *testing.T, name, fcmToken string) (*DeviceResponse, string) { + t.Helper() + keyPair := crypto.GenerateKeyPair(2048) devicePubKey := crypto.PublicKeyToBase64(keyPair.PublicKey) @@ -22,6 +24,8 @@ func CreateDevice(t *testing.T, name, fcmToken string) (*DeviceResponse, string) } func CreateBrowserExtension(t *testing.T, name string) *BrowserExtensionResponse { + t.Helper() + keyPair := crypto.GenerateKeyPair(2048) pubKey := crypto.PublicKeyToBase64(keyPair.PublicKey) @@ -36,6 +40,8 @@ func CreateBrowserExtension(t *testing.T, name string) *BrowserExtensionResponse } func CreateBrowserExtensionWithPublicKey(t *testing.T, name, publicKey string) *BrowserExtensionResponse { + t.Helper() + payload := []byte(fmt.Sprintf(`{"name":"%s","browser_name":"go-browser","browser_version":"0.1","public_key":"%s"}`, name, publicKey)) browserExt := new(BrowserExtensionResponse) @@ -46,6 +52,8 @@ func CreateBrowserExtensionWithPublicKey(t *testing.T, name, publicKey string) * } func PairDeviceWithBrowserExtension(t *testing.T, devicePubKey string, browserExtension *BrowserExtensionResponse, device *DeviceResponse) *PairingResultResponse { + t.Helper() + payload := struct { ExtensionId string `json:"extension_id"` DeviceName string `json:"device_name"` @@ -58,7 +66,10 @@ func PairDeviceWithBrowserExtension(t *testing.T, devicePubKey string, browserEx pairingResult := new(PairingResultResponse) - payloadJson, _ := json.Marshal(payload) + payloadJson, err := json.Marshal(payload) + if err != nil { + t.Fatalf("failed to marshal pairing payload: %v", err) + } DoAPISuccessPost(t, "/mobile/devices/"+device.Id+"/browser_extensions", payloadJson, pairingResult) @@ -66,6 +77,8 @@ func PairDeviceWithBrowserExtension(t *testing.T, devicePubKey string, browserEx } func GetExtensionDevices(t *testing.T, extensionId string) []*ExtensionPairedDeviceResponse { + t.Helper() + var extensionDevices []*ExtensionPairedDeviceResponse DoAPISuccessGet(t, "/browser_extensions/"+extensionId+"/devices", &extensionDevices) @@ -74,6 +87,8 @@ func GetExtensionDevices(t *testing.T, extensionId string) []*ExtensionPairedDev } func Request2FaToken(t *testing.T, domain, extensionId string) *AuthTokenRequestResponse { + t.Helper() + var response *AuthTokenRequestResponse payload := []byte(fmt.Sprintf(`{"domain":"%s"}`, domain)) @@ -84,39 +99,49 @@ func Request2FaToken(t *testing.T, domain, extensionId string) *AuthTokenRequest } func Send2FaTokenToExtension(t *testing.T, extensionId, deviceId, requestId, token string) { + t.Helper() + j := fmt.Sprintf(`{"token_request_id":"%s","extension_id":"%s","token":"%s"}`, requestId, extensionId, token) DoAPISuccessPost(t, "mobile/devices/"+deviceId+"/commands/send_2fa_token", []byte(j), nil) } func RemoveAllBrowserExtensionsDevices(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "browser_extensions/devices") } func RemoveAllBrowserExtensions(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "browser_extensions") } func RemoveAllMobileDevices(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "/mobile/devices") } func RemoveAllMobileIconsCollections(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "mobile/icons/collections") } func RemoveAllMobileWebServices(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "mobile/web_services") } func RemoveAllMobileIcons(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "mobile/icons") } func RemoveAllMobileIconsRequests(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "mobile/icons/requests") } func RemoveAllMobileNotifications(t *testing.T) { + t.Helper() DoAdminSuccessDelete(t, "mobile/notifications") } diff --git a/e2e-tests/http.go b/e2e-tests/http.go index 3da2f8f..ccf1106 100644 --- a/e2e-tests/http.go +++ b/e2e-tests/http.go @@ -32,11 +32,13 @@ func (a *BasicAuth) Header() string { } func DoAPISuccessPost(t *testing.T, uri string, payload []byte, resp interface{}) { + t.Helper() response := doRequest(t, apiRawURL, uri, http.MethodPost, payload, resp) require.Equal(t, http.StatusOK, response.StatusCode) } func DoAdminAPISuccessPost(t *testing.T, uri string, payload []byte, resp interface{}) { + t.Helper() response := doRequest(t, adminRawURL, uri, http.MethodPost, payload, resp) bb, err := io.ReadAll(response.Body) require.NoError(t, err) @@ -45,56 +47,69 @@ func DoAdminAPISuccessPost(t *testing.T, uri string, payload []byte, resp interf } func DoAdminPostAndAssertCode(t *testing.T, expCode int, uri string, payload []byte, resp interface{}) { + t.Helper() response := doRequest(t, adminRawURL, uri, http.MethodPost, payload, resp) require.Equal(t, expCode, response.StatusCode) } func DoAPIPostAndAssertCode(t *testing.T, expCode int, uri string, payload []byte, resp interface{}) { + t.Helper() response := doRequest(t, apiRawURL, uri, http.MethodPost, payload, resp) require.Equal(t, expCode, response.StatusCode) } func DoAPIRequest(t *testing.T, uri, method string, payload []byte, resp interface{}) *http.Response { + t.Helper() return doRequest(t, apiRawURL, uri, method, payload, resp) } func DoAdminRequest(t *testing.T, uri, method string, payload []byte, resp interface{}) *http.Response { + t.Helper() return doRequest(t, apiRawURL, uri, method, payload, resp) } func DoAdminSuccessPut(t *testing.T, uri string, payload []byte, resp interface{}) { + t.Helper() response := doRequest(t, adminRawURL, uri, http.MethodPut, payload, resp) bb, err := io.ReadAll(response.Body) require.NoError(t, err) - require.Equal(t, http.StatusOK, response.StatusCode, fmt.Sprintf("invalid status code, response payload is: %q", string(bb))) + require.Equal(t, http.StatusOK, response.StatusCode, "invalid status code, response payload is: %q", string(bb)) } func DoAPISuccessPut(t *testing.T, uri string, payload []byte, resp interface{}) { + t.Helper() + response := doRequest(t, apiRawURL, uri, http.MethodPut, payload, resp) require.Equal(t, http.StatusOK, response.StatusCode) } func DoAPISuccessGet(t *testing.T, uri string, resp interface{}) { + t.Helper() + response := doRequest(t, apiRawURL, uri, http.MethodGet, nil /*payload*/, resp) require.Equal(t, http.StatusOK, response.StatusCode) } func DoAPIGet(t *testing.T, uri string, resp interface{}) *http.Response { + t.Helper() return doRequest(t, apiRawURL, uri, http.MethodGet, nil /*payload*/, resp) } func DoAdminSuccessGet(t *testing.T, uri string, resp interface{}) { + t.Helper() response := doRequest(t, adminRawURL, uri, http.MethodGet, nil /*payload*/, resp) require.Equal(t, http.StatusOK, response.StatusCode) } func DoAdminSuccessDelete(t *testing.T, uri string) { + t.Helper() response := doRequest(t, adminRawURL, uri, http.MethodDelete, nil /*payload*/, nil /*response*/) require.Equal(t, http.StatusOK, response.StatusCode) } func DoAPISuccessDelete(t *testing.T, uri string) { + t.Helper() response := doRequest(t, apiRawURL, uri, http.MethodDelete, nil /*payload*/, nil /*response*/) require.Equal(t, http.StatusOK, response.StatusCode) } diff --git a/e2e-tests/icons/icons_collection_test.go b/e2e-tests/icons/icons_collection_test.go index b2fb7ba..130313a 100644 --- a/e2e-tests/icons/icons_collection_test.go +++ b/e2e-tests/icons/icons_collection_test.go @@ -3,9 +3,9 @@ package tests import ( "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) type iconsCollectionResponse struct { @@ -41,9 +41,9 @@ func (s *IconsCollectionsTestSuite) TestCreateIconsCollection() { var IconsCollection *iconsCollectionResponse e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/icons/collections", payload, &IconsCollection) - assert.Equal(s.T(), "facebook", IconsCollection.Name) - assert.Equal(s.T(), "desc", IconsCollection.Description) - assert.Equal(s.T(), []string{"123e4567-e89b-12d3-a456-426614174000"}, IconsCollection.Icons) + s.Equal("facebook", IconsCollection.Name) + s.Equal("desc", IconsCollection.Description) + s.Equal([]string{"123e4567-e89b-12d3-a456-426614174000"}, IconsCollection.Icons) } func (s *IconsCollectionsTestSuite) TestUpdateIconsCollection() { @@ -67,8 +67,8 @@ func (s *IconsCollectionsTestSuite) TestUpdateIconsCollection() { var updatedIconsCollection *iconsCollectionResponse e2e_tests.DoAdminSuccessPut(s.T(), "mobile/icons/collections/"+iconsCollection.Id, updatePayload, &updatedIconsCollection) - assert.Equal(s.T(), "meta", updatedIconsCollection.Name) - assert.Equal(s.T(), []string{"icon-1", "icon-2"}, updatedIconsCollection.Icons) + s.Equal("meta", updatedIconsCollection.Name) + s.Equal([]string{"icon-1", "icon-2"}, updatedIconsCollection.Icons) } func (s *IconsCollectionsTestSuite) TestDeleteIconsCollection() { @@ -84,7 +84,7 @@ func (s *IconsCollectionsTestSuite) TestDeleteIconsCollection() { e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/icons/collections/"+iconsCollection.Id) response := e2e_tests.DoAPIGet(s.T(), "mobile/icons/collections/"+iconsCollection.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *IconsCollectionsTestSuite) TestFindAllIconsCollections() { @@ -107,7 +107,7 @@ func (s *IconsCollectionsTestSuite) TestFindAllIconsCollections() { var IconsCollections []*iconsCollectionResponse e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons/collections", &IconsCollections) - assert.Len(s.T(), IconsCollections, 2) + s.Len(IconsCollections, 2) } func (s *IconsCollectionsTestSuite) TestFindIconsCollection() { @@ -124,5 +124,5 @@ func (s *IconsCollectionsTestSuite) TestFindIconsCollection() { var IconsCollection *iconsCollectionResponse e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons/collections/"+createdIconsCollection.Id, &IconsCollection) - assert.Equal(s.T(), "just-one", IconsCollection.Name) + s.Equal("just-one", IconsCollection.Name) } diff --git a/e2e-tests/icons/icons_requests_test.go b/e2e-tests/icons/icons_requests_test.go index 021800f..e82fde1 100644 --- a/e2e-tests/icons/icons_requests_test.go +++ b/e2e-tests/icons/icons_requests_test.go @@ -7,9 +7,9 @@ import ( "testing" "github.com/jaswdr/faker" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" "github.com/twofas/2fas-server/internal/api/icons/app/queries" ) @@ -31,8 +31,8 @@ func (s *IconsRequestsTestSuite) SetupTest() { func (s *IconsRequestsTestSuite) TestCreateIconRequest() { iconRequest := createIconRequest(s.T(), "service") - assert.Equal(s.T(), "service", iconRequest.ServiceName) - assert.Equal(s.T(), "desc", iconRequest.Description) + s.Equal("service", iconRequest.ServiceName) + s.Equal("desc", iconRequest.Description) } func (s *IconsRequestsTestSuite) TestCreateIconRequestWithNotAllowedIconDimensions() { @@ -67,7 +67,7 @@ func (s *IconsRequestsTestSuite) TestDeleteIconRequest() { e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/icons/requests/"+iconRequest.Id) response := e2e_tests.DoAPIGet(s.T(), "mobile/icons/requests/"+iconRequest.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *IconsRequestsTestSuite) TestFindAllIconsRequests() { @@ -77,7 +77,7 @@ func (s *IconsRequestsTestSuite) TestFindAllIconsRequests() { var iconsRequests []*queries.IconRequestPresenter e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons/requests", &iconsRequests) - assert.Len(s.T(), iconsRequests, 2) + s.Len(iconsRequests, 2) } func (s *IconsRequestsTestSuite) TestFindIconRequest() { @@ -86,7 +86,7 @@ func (s *IconsRequestsTestSuite) TestFindIconRequest() { var searchResult *queries.IconPresenter e2e_tests.DoAdminSuccessGet(s.T(), "mobile/icons/requests/"+iconRequest.Id, &searchResult) - assert.Equal(s.T(), "service", searchResult.Name) + s.Equal("service", searchResult.Name) } func (s *IconsRequestsTestSuite) TestTransformIconRequestIntoWebService() { @@ -95,7 +95,7 @@ func (s *IconsRequestsTestSuite) TestTransformIconRequestIntoWebService() { var result *queries.WebServicePresenter e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/icons/requests/"+iconRequest.Id+"/commands/transform_to_web_service", nil, &result) - assert.Equal(s.T(), "service", result.Name) + s.Equal("service", result.Name) } func (s *IconsRequestsTestSuite) TestTransformSingleIconRequestsIntoWebServiceFromManyRequestsWithSameServiceName() { @@ -108,7 +108,7 @@ func (s *IconsRequestsTestSuite) TestTransformSingleIconRequestsIntoWebServiceFr var icons []*queries.IconPresenter e2e_tests.DoAPIGet(s.T(), "mobile/icons", &icons) - assert.Len(s.T(), icons, 1) + s.Len(icons, 1) } func (s *IconsRequestsTestSuite) TestTransformIconRequestWithAlreadyExistingWebService() { @@ -127,16 +127,17 @@ func (s *IconsRequestsTestSuite) TestUpdateWebServiceFromIconRequest() { payload := []byte(`{"web_service_id":"` + webService.Id + `"}`) e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/icons/requests/"+iconRequest.Id+"/commands/update_web_service", payload, &result) - assert.Equal(s.T(), webService.Name, result.Name) + s.Equal(webService.Name, result.Name) var iconsCollections []string if err := json.Unmarshal(result.IconsCollections, &iconsCollections); err != nil { - assert.NoError(s.T(), err) + s.NoError(err) } - assert.Equal(s.T(), webService.IconsCollections, iconsCollections, "icons collections id should not change") + s.Equal(webService.IconsCollections, iconsCollections, "icons collections id should not change") } func createIconRequest(t *testing.T, serviceName string) *queries.IconRequestPresenter { + t.Helper() if serviceName == "" { serviceName = "some-service" } diff --git a/e2e-tests/icons/icons_test.go b/e2e-tests/icons/icons_test.go index 1806aa4..b0b48e8 100644 --- a/e2e-tests/icons/icons_test.go +++ b/e2e-tests/icons/icons_test.go @@ -6,13 +6,14 @@ import ( "testing" "github.com/jaswdr/faker" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" query "github.com/twofas/2fas-server/internal/api/icons/app/queries" ) func createIcon(t *testing.T) *query.IconPresenter { + t.Helper() img := faker.New().Image().Image(120, 120) pngImg, err := ioutil.ReadFile(img.Name()) @@ -54,7 +55,7 @@ func (s *IconsTestSuite) SetupTest() { func (s *IconsTestSuite) TestCreateIcon() { icon := createIcon(s.T()) - assert.Equal(s.T(), "facebook", icon.Name) + s.Equal("facebook", icon.Name) } func (s *IconsTestSuite) TestUpdateIcon() { @@ -69,7 +70,7 @@ func (s *IconsTestSuite) TestUpdateIcon() { var updatedIcon *query.IconPresenter e2e_tests.DoAdminSuccessPut(s.T(), "mobile/icons/"+icon.Id, updatePayload, &updatedIcon) - assert.Equal(s.T(), "meta", updatedIcon.Name) + s.Equal("meta", updatedIcon.Name) } func (s *IconsTestSuite) TestDeleteIcon() { @@ -78,7 +79,7 @@ func (s *IconsTestSuite) TestDeleteIcon() { e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/icons/"+icon.Id) response := e2e_tests.DoAPIGet(s.T(), "mobile/icons/"+icon.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *IconsTestSuite) TestFindAllIcons() { @@ -88,7 +89,7 @@ func (s *IconsTestSuite) TestFindAllIcons() { var Icons []*query.IconPresenter e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons", &Icons) - assert.Len(s.T(), Icons, 2) + s.Len(Icons, 2) } func (s *IconsTestSuite) TestFindIcon() { @@ -97,5 +98,5 @@ func (s *IconsTestSuite) TestFindIcon() { var searchResult *query.IconPresenter e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons/"+icon.Id, &searchResult) - assert.Equal(s.T(), "facebook", searchResult.Name) + s.Equal("facebook", searchResult.Name) } diff --git a/e2e-tests/icons/web_services_dump_test.go b/e2e-tests/icons/web_services_dump_test.go index 15a7a81..7d86e73 100644 --- a/e2e-tests/icons/web_services_dump_test.go +++ b/e2e-tests/icons/web_services_dump_test.go @@ -5,9 +5,9 @@ import ( "math/rand" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestWebServicesDumpTestSuite(t *testing.T) { @@ -30,10 +30,11 @@ func (s *WebServicesDumpTestSuite) TestWebServicesDump() { response := e2e_tests.DoAPIGet(s.T(), "mobile/web_services/dump", nil) - assert.Equal(s.T(), 200, response.StatusCode) + s.Equal(200, response.StatusCode) } func createWebService(t *testing.T) *webServiceResponse { + t.Helper() iconsCollection := createIconsCollection(t) payload := []byte(` @@ -54,6 +55,7 @@ func createWebService(t *testing.T) *webServiceResponse { } func createIconsCollection(t *testing.T) *iconsCollectionResponse { + t.Helper() icon := createIcon(t) payload := []byte(` diff --git a/e2e-tests/icons/web_services_test.go b/e2e-tests/icons/web_services_test.go index 2e23e57..317f8e2 100644 --- a/e2e-tests/icons/web_services_test.go +++ b/e2e-tests/icons/web_services_test.go @@ -3,9 +3,9 @@ package tests import ( "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" "github.com/twofas/2fas-server/internal/api/icons/app/command" ) @@ -47,11 +47,11 @@ func (s *WebServicesTestSuite) TestCreateWebService() { var webService *webServiceResponse e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/web_services", payload, &webService) - assert.Equal(s.T(), "facebook", webService.Name) - assert.Equal(s.T(), "desc", webService.Description) - assert.Equal(s.T(), []string{"facebook", "meta"}, webService.Issuers) - assert.Equal(s.T(), []string{"shitbook"}, webService.Tags) - assert.Equal(s.T(), []string{"123e4567-e89b-12d3-a456-426614174000"}, webService.IconsCollections) + s.Equal("facebook", webService.Name) + s.Equal("desc", webService.Description) + s.Equal([]string{"facebook", "meta"}, webService.Issuers) + s.Equal([]string{"shitbook"}, webService.Tags) + s.Equal([]string{"123e4567-e89b-12d3-a456-426614174000"}, webService.IconsCollections) } func (s *WebServicesTestSuite) TestCreateWebServiceWithAlreadyExistingName() { @@ -82,7 +82,7 @@ func (s *WebServicesTestSuite) TestCreateWebServiceWithMatchRules() { var webService *webServiceResponse e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/web_services", payload, &webService) - assert.Equal(s.T(), []*command.MatchRule{{ + s.Equal([]*command.MatchRule{{ Field: "label", Text: "facebook.com", Matcher: "contains", @@ -114,10 +114,10 @@ func (s *WebServicesTestSuite) TestUpdateWebService() { var updatedWebService *webServiceResponse e2e_tests.DoAdminSuccessPut(s.T(), "mobile/web_services/"+webService.Id, updatePayload, &updatedWebService) - assert.Equal(s.T(), "meta", updatedWebService.Name) - assert.Equal(s.T(), []string{"meta", "facebook"}, updatedWebService.Issuers) - assert.Equal(s.T(), []string{"tag1", "tag2"}, updatedWebService.Tags) - assert.Equal(s.T(), []string{"set1", "set2"}, updatedWebService.IconsCollections) + s.Equal("meta", updatedWebService.Name) + s.Equal([]string{"meta", "facebook"}, updatedWebService.Issuers) + s.Equal([]string{"tag1", "tag2"}, updatedWebService.Tags) + s.Equal([]string{"set1", "set2"}, updatedWebService.IconsCollections) } func (s *WebServicesTestSuite) TestUpdateWebServiceMatchRule() { @@ -143,10 +143,10 @@ func (s *WebServicesTestSuite) TestUpdateWebServiceMatchRule() { var updatedWebService *webServiceResponse e2e_tests.DoAdminSuccessPut(s.T(), "mobile/web_services/"+webService.Id, updatePayload, &updatedWebService) - assert.Equal(s.T(), "issuer", updatedWebService.MatchRules[0].Field) - assert.Equal(s.T(), "facebook.pl", updatedWebService.MatchRules[0].Text) - assert.Equal(s.T(), "starts_with", updatedWebService.MatchRules[0].Matcher) - assert.Equal(s.T(), false, updatedWebService.MatchRules[0].IgnoreCase) + s.Equal("issuer", updatedWebService.MatchRules[0].Field) + s.Equal("facebook.pl", updatedWebService.MatchRules[0].Text) + s.Equal("starts_with", updatedWebService.MatchRules[0].Matcher) + s.False(updatedWebService.MatchRules[0].IgnoreCase) } func (s *WebServicesTestSuite) TestDeleteWebService() { @@ -165,7 +165,7 @@ func (s *WebServicesTestSuite) TestDeleteWebService() { e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/web_services/"+webService.Id) response := e2e_tests.DoAPIGet(s.T(), "mobile/web_services/"+webService.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *WebServicesTestSuite) TestFindAllWebServices() { @@ -193,7 +193,7 @@ func (s *WebServicesTestSuite) TestFindAllWebServices() { var webServices []*webServiceResponse e2e_tests.DoAPISuccessGet(s.T(), "mobile/web_services", &webServices) - assert.Len(s.T(), webServices, 2) + s.Len(webServices, 2) } func (s *WebServicesTestSuite) TestFindWebService() { @@ -212,5 +212,5 @@ func (s *WebServicesTestSuite) TestFindWebService() { var webService *webServiceResponse e2e_tests.DoAPISuccessGet(s.T(), "mobile/web_services/"+createdWebService.Id, &webService) - assert.Equal(s.T(), "just-one", webService.Name) + s.Equal("just-one", webService.Name) } diff --git a/e2e-tests/mobile/mobile_browser_extensions_2fa_requests_test.go b/e2e-tests/mobile/mobile_browser_extensions_2fa_requests_test.go index b9f813e..32936ad 100644 --- a/e2e-tests/mobile/mobile_browser_extensions_2fa_requests_test.go +++ b/e2e-tests/mobile/mobile_browser_extensions_2fa_requests_test.go @@ -3,9 +3,9 @@ package tests import ( "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestMobileDeviceExtensionIntegrationTestSuite(t *testing.T) { @@ -33,7 +33,7 @@ func (s *MobileDeviceExtensionIntegrationTestSuite) TestGetPending2FaRequests() var tokenRequestsCollection []*e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "mobile/devices/"+device.Id+"/browser_extensions/2fa_requests", &tokenRequestsCollection) - assert.Len(s.T(), tokenRequestsCollection, 1) + s.Len(tokenRequestsCollection, 1) } func (s *MobileDeviceExtensionIntegrationTestSuite) TestDoNotReturnCompleted2FaRequests() { @@ -50,5 +50,5 @@ func (s *MobileDeviceExtensionIntegrationTestSuite) TestDoNotReturnCompleted2FaR var tokenRequestsCollection []*e2e_tests.AuthTokenRequestResponse e2e_tests.DoAPISuccessGet(s.T(), "mobile/devices/"+device.Id+"/browser_extensions/2fa_requests", &tokenRequestsCollection) - assert.Len(s.T(), tokenRequestsCollection, 0) + s.Empty(tokenRequestsCollection) } diff --git a/e2e-tests/mobile/mobile_device_extension_test.go b/e2e-tests/mobile/mobile_device_extension_test.go index 07052ed..411eb24 100644 --- a/e2e-tests/mobile/mobile_device_extension_test.go +++ b/e2e-tests/mobile/mobile_device_extension_test.go @@ -5,9 +5,9 @@ import ( "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestMobileDeviceExtensionTestSuite(t *testing.T) { @@ -29,7 +29,7 @@ func (s *MobileDeviceExtensionTestSuite) TestDoNotFindExtensionsForNotExistingDe response := e2e_tests.DoAPIGet(s.T(), "/mobile/devices/"+notExistingDeviceId.String()+"/browser_extensions", nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *MobileDeviceExtensionTestSuite) TestDoNotFindNotExistingMobileDeviceExtension() { @@ -40,7 +40,7 @@ func (s *MobileDeviceExtensionTestSuite) TestDoNotFindNotExistingMobileDeviceExt notExistingExtensionId := uuid.New() response := e2e_tests.DoAPIGet(s.T(), "/mobile/devices/"+device.Id+"/browser_extensions/"+notExistingExtensionId.String(), nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *MobileDeviceExtensionTestSuite) Test_FindExtensionForDevice() { @@ -51,7 +51,7 @@ func (s *MobileDeviceExtensionTestSuite) Test_FindExtensionForDevice() { var deviceBrowserExtension *e2e_tests.BrowserExtensionResponse e2e_tests.DoAPISuccessGet(s.T(), "/mobile/devices/"+device.Id+"/browser_extensions/"+browserExt.Id, &deviceBrowserExtension) - assert.Equal(s.T(), browserExt.Id, deviceBrowserExtension.Id) + s.Equal(browserExt.Id, deviceBrowserExtension.Id) } func (s *MobileDeviceExtensionTestSuite) Test_FindAllDeviceExtensions() { @@ -65,7 +65,7 @@ func (s *MobileDeviceExtensionTestSuite) Test_FindAllDeviceExtensions() { var deviceBrowserExtensions []*e2e_tests.BrowserExtensionResponse e2e_tests.DoAPISuccessGet(s.T(), "/mobile/devices/"+device.Id+"/browser_extensions/", &deviceBrowserExtensions) - assert.Len(s.T(), deviceBrowserExtensions, 2) + s.Len(deviceBrowserExtensions, 2) } func (s *MobileDeviceExtensionTestSuite) Test_DisconnectExtensionFromDevice() { @@ -79,11 +79,11 @@ func (s *MobileDeviceExtensionTestSuite) Test_DisconnectExtensionFromDevice() { var deviceBrowserExtension1 *e2e_tests.BrowserExtensionResponse response := e2e_tests.DoAPIGet(s.T(), "/mobile/devices/"+device.Id+"/browser_extensions/"+browserExt1.Id, &deviceBrowserExtension1) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) var deviceBrowserExtension2 *e2e_tests.BrowserExtensionResponse e2e_tests.DoAPISuccessGet(s.T(), "/mobile/devices/"+device.Id+"/browser_extensions/"+browserExt2.Id, &deviceBrowserExtension2) - assert.Equal(s.T(), browserExt2.Id, deviceBrowserExtension2.Id) + s.Equal(browserExt2.Id, deviceBrowserExtension2.Id) } func (s *MobileDeviceExtensionTestSuite) TestExtensionHasAlreadyBeenConnected() { diff --git a/e2e-tests/mobile/mobile_device_test.go b/e2e-tests/mobile/mobile_device_test.go index 8929ed5..ff14655 100644 --- a/e2e-tests/mobile/mobile_device_test.go +++ b/e2e-tests/mobile/mobile_device_test.go @@ -5,9 +5,9 @@ import ( "net/http" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func TestMobileDeviceTestSuite(t *testing.T) { @@ -43,11 +43,12 @@ func (s *MobileDeviceTestSuite) TestCreateMobileDevice() { for _, tc := range testsCases { response := createDevice(s.T(), tc.deviceName, tc.fcmToken) - assert.Equal(s.T(), tc.expectedHttpCode, response.StatusCode) + s.Equal(tc.expectedHttpCode, response.StatusCode) } } func createDevice(t *testing.T, name, fcmToken string) *http.Response { + t.Helper() payload := []byte(fmt.Sprintf(`{"name":"%s","platform":"android","fcm_token":"%s"}`, name, fcmToken)) return e2e_tests.DoAPIRequest(t, "mobile/devices", http.MethodPost, payload, nil) } diff --git a/e2e-tests/mobile/mobile_notifications_test.go b/e2e-tests/mobile/mobile_notifications_test.go index 98f5a43..e25d4b3 100644 --- a/e2e-tests/mobile/mobile_notifications_test.go +++ b/e2e-tests/mobile/mobile_notifications_test.go @@ -5,9 +5,9 @@ import ( "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" query "github.com/twofas/2fas-server/internal/api/mobile/app/queries" ) @@ -30,11 +30,11 @@ func (s *MobileNotificationsTestSuite) TestCreateMobileNotification() { e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/notifications", payload, ¬ification) - assert.Equal(s.T(), "android", notification.Platform) - assert.Equal(s.T(), "0.1", notification.Version) - assert.Equal(s.T(), "2fas.com", notification.Link) - assert.Equal(s.T(), "demo", notification.Message) - assert.Equal(s.T(), "features", notification.Icon) + s.Equal("android", notification.Platform) + s.Equal("0.1", notification.Version) + s.Equal("2fas.com", notification.Link) + s.Equal("demo", notification.Message) + s.Equal("features", notification.Icon) } func (s *MobileNotificationsTestSuite) TestUpdateMobileNotification() { @@ -46,11 +46,11 @@ func (s *MobileNotificationsTestSuite) TestUpdateMobileNotification() { var updatedNotification *query.MobileNotificationPresenter e2e_tests.DoAdminSuccessPut(s.T(), "mobile/notifications/"+notification.Id, payload, &updatedNotification) - assert.Equal(s.T(), "ios", updatedNotification.Platform) - assert.Equal(s.T(), "1.1", updatedNotification.Version) - assert.Equal(s.T(), "new-2fas.com", updatedNotification.Link) - assert.Equal(s.T(), "new-demo", updatedNotification.Message) - assert.Equal(s.T(), "youtube", updatedNotification.Icon) + s.Equal("ios", updatedNotification.Platform) + s.Equal("1.1", updatedNotification.Version) + s.Equal("new-2fas.com", updatedNotification.Link) + s.Equal("new-demo", updatedNotification.Message) + s.Equal("youtube", updatedNotification.Icon) } func (s *MobileNotificationsTestSuite) TestDeleteMobileNotification() { @@ -61,7 +61,7 @@ func (s *MobileNotificationsTestSuite) TestDeleteMobileNotification() { e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/notifications/"+notification.Id) response := e2e_tests.DoAPIGet(s.T(), "mobile/notifications/"+notification.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *MobileNotificationsTestSuite) TestDeleteNotExistingMobileNotification() { @@ -69,7 +69,7 @@ func (s *MobileNotificationsTestSuite) TestDeleteNotExistingMobileNotification() response := e2e_tests.DoAPIRequest(s.T(), "mobile/notifications/"+id.String(), http.MethodDelete, nil /*payload*/, nil /*resp*/) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *MobileNotificationsTestSuite) TestFindAllNotifications() { @@ -84,7 +84,7 @@ func (s *MobileNotificationsTestSuite) TestFindAllNotifications() { var collection []*query.MobileNotificationPresenter e2e_tests.DoAPISuccessGet(s.T(), "mobile/notifications", &collection) - assert.Len(s.T(), collection, 2) + s.Len(collection, 2) } func (s *MobileNotificationsTestSuite) TestDoNotFindNotifications() { @@ -92,7 +92,7 @@ func (s *MobileNotificationsTestSuite) TestDoNotFindNotifications() { e2e_tests.DoAPISuccessGet(s.T(), "mobile/notifications", &collection) - assert.Len(s.T(), collection, 0) + s.Empty(collection) } func (s *MobileNotificationsTestSuite) TestPublishNotification() { @@ -103,5 +103,5 @@ func (s *MobileNotificationsTestSuite) TestPublishNotification() { var publishedNotification *query.MobileNotificationPresenter e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/notifications/"+notification.Id+"/commands/publish", payload, &publishedNotification) - assert.NotEmpty(s.T(), "published_at", notification.PublishedAt) + s.NotEmpty(publishedNotification.PublishedAt, "published_at") } diff --git a/e2e-tests/mobile/mobile_security_test.go b/e2e-tests/mobile/mobile_security_test.go index 283b4d2..ccb546b 100644 --- a/e2e-tests/mobile/mobile_security_test.go +++ b/e2e-tests/mobile/mobile_security_test.go @@ -6,8 +6,9 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/require" - "github.com/twofas/2fas-server/e2e-tests" "golang.org/x/sync/errgroup" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func Test_MobileApiBandwidthAbuse(t *testing.T) { diff --git a/e2e-tests/support/mobile_debug_logs_test.go b/e2e-tests/support/mobile_debug_logs_test.go index 1124dda..3cfc131 100644 --- a/e2e-tests/support/mobile_debug_logs_test.go +++ b/e2e-tests/support/mobile_debug_logs_test.go @@ -11,8 +11,6 @@ import ( "testing" "github.com/google/uuid" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" e2e_tests "github.com/twofas/2fas-server/e2e-tests" @@ -38,8 +36,8 @@ func (s *DebugLogsAuditTestSuite) TestCreateDebugLogsAuditClaim() { e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/support/debug_logs/audit/claim", payload, auditClaim) - assert.Equal(s.T(), "app-user", auditClaim.Username) - assert.Equal(s.T(), "some description", auditClaim.Description) + s.Equal("app-user", auditClaim.Username) + s.Equal("some description", auditClaim.Description) } func (s *DebugLogsAuditTestSuite) TestUpdateDebugLogsAuditClaim() { @@ -49,8 +47,8 @@ func (s *DebugLogsAuditTestSuite) TestUpdateDebugLogsAuditClaim() { updatePayload := []byte(`{"username": "app-user-1", "description": "another description"}`) e2e_tests.DoAdminSuccessPut(s.T(), "mobile/support/debug_logs/audit/claim/"+auditClaim.Id, updatePayload, &updatedAuditClaim) - assert.Equal(s.T(), "app-user-1", updatedAuditClaim.Username) - assert.Equal(s.T(), "another description", updatedAuditClaim.Description) + s.Equal("app-user-1", updatedAuditClaim.Username) + s.Equal("another description", updatedAuditClaim.Description) } func (s *DebugLogsAuditTestSuite) TestFulfillDebugLogsAuditClaim() { @@ -64,12 +62,12 @@ func (s *DebugLogsAuditTestSuite) TestFulfillDebugLogsAuditClaim() { writer.Close() - request, _ := http.NewRequest("POST", "http://localhost/mobile/support/debug_logs/audit/"+auditClaim.Id, body) + request, _ := http.NewRequest(http.MethodPost, "http://localhost/mobile/support/debug_logs/audit/"+auditClaim.Id, body) request.Header.Add("Content-Type", writer.FormDataContentType()) response, err := http.DefaultClient.Do(request) - require.NoError(s.T(), err) - assert.Equal(s.T(), 200, response.StatusCode) + s.Require().NoError(err) + s.Equal(200, response.StatusCode) reqB, _ := ioutil.ReadAll(body) s.T().Log(string(reqB)) @@ -102,42 +100,42 @@ func (s *DebugLogsAuditTestSuite) TestTryToFulfillDebugLogsAuditClaimTwice() { auditClaim := createDebugLogsAuditClaim(s.T(), "user1", "desc1") body, formDataContentType, err := mkFormFileBody() - assert.Nil(s.T(), err) + s.Require().NoError(err) - request, _ := http.NewRequest("POST", "http://localhost/mobile/support/debug_logs/audit/"+auditClaim.Id, body) + request, _ := http.NewRequest(http.MethodPost, "http://localhost/mobile/support/debug_logs/audit/"+auditClaim.Id, body) request.Header.Add("Content-Type", formDataContentType) response, err := http.DefaultClient.Do(request) - require.NoError(s.T(), err) - assert.Equal(s.T(), 200, response.StatusCode) + s.Require().NoError(err) + s.Equal(200, response.StatusCode) body, formDataContentType, err = mkFormFileBody() - assert.Nil(s.T(), err) - secondRequest, _ := http.NewRequest("POST", "http://localhost/mobile/support/debug_logs/audit/"+auditClaim.Id, body) + s.Require().NoError(err) + secondRequest, _ := http.NewRequest(http.MethodPost, "http://localhost/mobile/support/debug_logs/audit/"+auditClaim.Id, body) secondRequest.Header.Add("Content-Type", formDataContentType) secondResponse, err := http.DefaultClient.Do(secondRequest) - require.NoError(s.T(), err) + s.Require().NoError(err) responseBody, err := io.ReadAll(secondResponse.Body) - require.NoError(s.T(), err) + s.Require().NoError(err) - assert.Equal(s.T(), 410, secondResponse.StatusCode, "Response body: %s", string(responseBody)) + s.Equal(410, secondResponse.StatusCode, "Response body: %s", string(responseBody)) } func (s *DebugLogsAuditTestSuite) TestTryToFulfillNotExistingDebugLogsAuditClaim() { notExistingAuditClaimId := uuid.New().String() body, formDataContentType, err := mkFormFileBody() - assert.Nil(s.T(), err) + s.Require().NoError(err) - request, _ := http.NewRequest("POST", "http://localhost/mobile/support/debug_logs/audit/"+notExistingAuditClaimId, body) + request, _ := http.NewRequest(http.MethodPost, "http://localhost/mobile/support/debug_logs/audit/"+notExistingAuditClaimId, body) request.Header.Add("Content-Type", formDataContentType) response, err := http.DefaultClient.Do(request) - require.NoError(s.T(), err) + s.Require().NoError(err) responseBody, err := io.ReadAll(response.Body) - require.NoError(s.T(), err) + s.Require().NoError(err) - assert.Equal(s.T(), 404, response.StatusCode, "Response body: %s", string(responseBody)) + s.Equal(404, response.StatusCode, "Response body: %s", string(responseBody)) } func (s *DebugLogsAuditTestSuite) TestTryToFulfillDebugLogsAuditClaimUsingInvalidId() { @@ -145,14 +143,14 @@ func (s *DebugLogsAuditTestSuite) TestTryToFulfillDebugLogsAuditClaimUsingInvali invalidId := strings.ToUpper(auditClaimId) body, formDataContentType, err := mkFormFileBody() - assert.Nil(s.T(), err) + s.Require().NoError(err) - request, _ := http.NewRequest("POST", "http://localhost/mobile/support/debug_logs/audit/"+invalidId, body) + request, _ := http.NewRequest(http.MethodPost, "http://localhost/mobile/support/debug_logs/audit/"+invalidId, body) request.Header.Add("Content-Type", formDataContentType) response, err := http.DefaultClient.Do(request) - require.NoError(s.T(), err) + s.Require().NoError(err) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *DebugLogsAuditTestSuite) TestGetDebugLogsAudit() { @@ -161,9 +159,9 @@ func (s *DebugLogsAuditTestSuite) TestGetDebugLogsAudit() { audit := new(query.DebugLogsAuditPresenter) e2e_tests.DoAdminSuccessGet(s.T(), "mobile/support/debug_logs/audit/"+auditClaim.Id, audit) - assert.Equal(s.T(), auditClaim.Id, audit.Id) - assert.Equal(s.T(), "user1", audit.Username) - assert.Equal(s.T(), "desc1", audit.Description) + s.Equal(auditClaim.Id, audit.Id) + s.Equal("user1", audit.Username) + s.Equal("desc1", audit.Description) } func (s *DebugLogsAuditTestSuite) TestDeleteDebugLogsAudit() { @@ -172,7 +170,7 @@ func (s *DebugLogsAuditTestSuite) TestDeleteDebugLogsAudit() { e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/support/debug_logs/audit/"+auditClaim.Id) response := e2e_tests.DoAPIGet(s.T(), "mobile/support/debug_logs/audit/"+auditClaim.Id, nil) - assert.Equal(s.T(), 404, response.StatusCode) + s.Equal(404, response.StatusCode) } func (s *DebugLogsAuditTestSuite) TestFindAllDebugLogsAudit() { @@ -182,10 +180,11 @@ func (s *DebugLogsAuditTestSuite) TestFindAllDebugLogsAudit() { var audits []*query.DebugLogsAuditPresenter e2e_tests.DoAdminSuccessGet(s.T(), "mobile/support/debug_logs/audit", &audits) - assert.Len(s.T(), audits, 2) + s.Len(audits, 2) } func createDebugLogsAuditClaim(t *testing.T, username, description string) *query.DebugLogsAuditPresenter { + t.Helper() payload := []byte(`{"username": "` + username + `", "description": "` + description + `"}`) auditClaim := new(query.DebugLogsAuditPresenter) diff --git a/e2e-tests/system/api_test.go b/e2e-tests/system/api_test.go index dc45d4a..c51950c 100644 --- a/e2e-tests/system/api_test.go +++ b/e2e-tests/system/api_test.go @@ -6,7 +6,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/twofas/2fas-server/e2e-tests" + + e2e_tests "github.com/twofas/2fas-server/e2e-tests" ) func Test_Default404Response(t *testing.T) { diff --git a/e2e-tests/websocket.go b/e2e-tests/websocket.go index 282faea..fccc2b2 100644 --- a/e2e-tests/websocket.go +++ b/e2e-tests/websocket.go @@ -1,11 +1,12 @@ package e2e_tests import ( - "github.com/gorilla/websocket" - "github.com/stretchr/testify/assert" "log" "net/url" "testing" + + "github.com/gorilla/websocket" + "github.com/stretchr/testify/assert" ) type WebsocketTestListener struct { @@ -51,5 +52,6 @@ func (l *WebsocketTestListener) StartListening() *websocket.Conn { } func (l *WebsocketTestListener) AssertMessageHasBeenReceived(t *testing.T, expected string) { + t.Helper() assert.JSONEq(t, expected, <-l.ReceivedMessages) } diff --git a/internal/api/browser_extension/adapters/browser_extension_2fa_request_mysql_repository.go b/internal/api/browser_extension/adapters/browser_extension_2fa_request_mysql_repository.go index 4a37d96..53e9f76 100644 --- a/internal/api/browser_extension/adapters/browser_extension_2fa_request_mysql_repository.go +++ b/internal/api/browser_extension/adapters/browser_extension_2fa_request_mysql_repository.go @@ -2,17 +2,19 @@ package adapters import ( "fmt" + "github.com/google/uuid" "github.com/pkg/errors" - "github.com/twofas/2fas-server/internal/api/browser_extension/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) -type TokenRequestCouldNotBeFound struct { +type TokenRequestCouldNotBeFoundError struct { RequestId string } -func (e TokenRequestCouldNotBeFound) Error() string { +func (e TokenRequestCouldNotBeFoundError) Error() string { return fmt.Sprintf("Token request could not be found: %s", e.RequestId) } @@ -62,7 +64,7 @@ func (r *BrowserExtension2FaRequestsMysqlRepository) FindById(tokenRequestId, ex result := r.db.First(&request, "extension_id = ? AND id = ?", extensionId.String(), tokenRequestId.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, TokenRequestCouldNotBeFound{RequestId: tokenRequestId.String()} + return nil, TokenRequestCouldNotBeFoundError{RequestId: tokenRequestId.String()} } return request, nil diff --git a/internal/api/browser_extension/adapters/browser_extension_devices_mysql_repository.go b/internal/api/browser_extension/adapters/browser_extension_devices_mysql_repository.go index 238da80..66aa6ef 100644 --- a/internal/api/browser_extension/adapters/browser_extension_devices_mysql_repository.go +++ b/internal/api/browser_extension/adapters/browser_extension_devices_mysql_repository.go @@ -2,18 +2,20 @@ package adapters import ( "fmt" + "github.com/doug-martin/goqu/v9" "github.com/google/uuid" "github.com/pkg/errors" - "github.com/twofas/2fas-server/internal/api/browser_extension/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) -type ExtensionDeviceCouldNotBeFound struct { +type ExtensionDeviceCouldNotBeFoundError struct { DeviceId string } -func (d ExtensionDeviceCouldNotBeFound) Error() string { +func (d ExtensionDeviceCouldNotBeFoundError) Error() string { return fmt.Sprintf("Extension device could not be found: %s", d.DeviceId) } @@ -89,7 +91,7 @@ func (r *BrowserExtensionDevicesMysqlRepository) GetById(extensionId, deviceId u result := r.db.Raw(sql).First(&device) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, ExtensionDeviceCouldNotBeFound{DeviceId: deviceId.String()} + return nil, ExtensionDeviceCouldNotBeFoundError{DeviceId: deviceId.String()} } return device, nil diff --git a/internal/api/browser_extension/adapters/browser_extension_mysql_repository.go b/internal/api/browser_extension/adapters/browser_extension_mysql_repository.go index b083d26..16dd76c 100644 --- a/internal/api/browser_extension/adapters/browser_extension_mysql_repository.go +++ b/internal/api/browser_extension/adapters/browser_extension_mysql_repository.go @@ -3,16 +3,18 @@ package adapters import ( "errors" "fmt" + "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/browser_extension/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) -type BrowserExtensionsCouldNotBeFound struct { +type BrowserExtensionsCouldNotBeFoundError struct { ExtensionId string } -func (e BrowserExtensionsCouldNotBeFound) Error() string { +func (e BrowserExtensionsCouldNotBeFoundError) Error() string { return fmt.Sprintf("Extension could not be found: %s", e.ExtensionId) } @@ -46,7 +48,7 @@ func (r *BrowserExtensionsMysqlRepository) FindById(id uuid.UUID) (*domain.Brows result := r.db.First(&extension, "id = ?", id.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, BrowserExtensionsCouldNotBeFound{ExtensionId: id.String()} + return nil, BrowserExtensionsCouldNotBeFoundError{ExtensionId: id.String()} } return extension, nil diff --git a/internal/api/browser_extension/app/command/close_2fa_request.go b/internal/api/browser_extension/app/command/close_2fa_request.go index 59e7069..7b13fc0 100644 --- a/internal/api/browser_extension/app/command/close_2fa_request.go +++ b/internal/api/browser_extension/app/command/close_2fa_request.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) diff --git a/internal/api/browser_extension/app/command/register_browser_extension.go b/internal/api/browser_extension/app/command/register_browser_extension.go index 7ffc3bd..101733d 100644 --- a/internal/api/browser_extension/app/command/register_browser_extension.go +++ b/internal/api/browser_extension/app/command/register_browser_extension.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) diff --git a/internal/api/browser_extension/app/command/remove_extension_paired_device.go b/internal/api/browser_extension/app/command/remove_extension_paired_device.go index d3831a3..0066364 100644 --- a/internal/api/browser_extension/app/command/remove_extension_paired_device.go +++ b/internal/api/browser_extension/app/command/remove_extension_paired_device.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) diff --git a/internal/api/browser_extension/app/command/update_browser_extension.go b/internal/api/browser_extension/app/command/update_browser_extension.go index 79de380..37ba837 100644 --- a/internal/api/browser_extension/app/command/update_browser_extension.go +++ b/internal/api/browser_extension/app/command/update_browser_extension.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) diff --git a/internal/api/browser_extension/app/query/browser_extension_2fa_requests_query.go b/internal/api/browser_extension/app/query/browser_extension_2fa_requests_query.go index 4855b27..1b2e43f 100644 --- a/internal/api/browser_extension/app/query/browser_extension_2fa_requests_query.go +++ b/internal/api/browser_extension/app/query/browser_extension_2fa_requests_query.go @@ -2,8 +2,9 @@ package query import ( "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/api/browser_extension/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) type BrowserExtension2FaRequestPresenter struct { diff --git a/internal/api/browser_extension/app/security/middleware.go b/internal/api/browser_extension/app/security/middleware.go index 54c054d..010c0c6 100644 --- a/internal/api/browser_extension/app/security/middleware.go +++ b/internal/api/browser_extension/app/security/middleware.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/twofas/2fas-server/internal/common/logging" "github.com/twofas/2fas-server/internal/common/rate_limit" ) diff --git a/internal/api/browser_extension/ports/http.go b/internal/api/browser_extension/ports/http.go index 83fc0a6..435c119 100644 --- a/internal/api/browser_extension/ports/http.go +++ b/internal/api/browser_extension/ports/http.go @@ -112,8 +112,8 @@ func (r *RoutesHandler) RemovePairedDeviceFromExtension(c *gin.Context) { err := r.cqrs.Commands.RemoveExtensionPairedDevice.Handle(cmd) if err != nil { - var extensionNotFoundErr adapters.BrowserExtensionsCouldNotBeFound - var deviceNotFoundErr adapters.ExtensionDeviceCouldNotBeFound + var extensionNotFoundErr adapters.BrowserExtensionsCouldNotBeFoundError + var deviceNotFoundErr adapters.ExtensionDeviceCouldNotBeFoundError if errors.As(err, &deviceNotFoundErr) || errors.As(err, &extensionNotFoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -145,7 +145,7 @@ func (r *RoutesHandler) RemoveAllExtensionPairedDevices(c *gin.Context) { err := r.cqrs.Commands.RemoveAllExtensionPairedDevices.Handle(cmd) if err != nil { - var notFoundErr adapters.BrowserExtensionsCouldNotBeFound + var notFoundErr adapters.BrowserExtensionsCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -261,7 +261,7 @@ func (r *RoutesHandler) UpdateBrowserExtension(c *gin.Context) { err := r.cqrs.Commands.UpdateBrowserExtension.Handle(cmd) if err != nil { - var notFoundErr adapters.BrowserExtensionsCouldNotBeFound + var notFoundErr adapters.BrowserExtensionsCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -277,7 +277,7 @@ func (r *RoutesHandler) UpdateBrowserExtension(c *gin.Context) { presenter, err := r.cqrs.Queries.BrowserExtensionQuery.Handle(queryCmd) - if errors.Is(err, adapters.BrowserExtensionsCouldNotBeFound{}) { + if errors.Is(err, adapters.BrowserExtensionsCouldNotBeFoundError{}) { c.JSON(404, api.NotFoundError(err)) return @@ -354,7 +354,7 @@ func (r *RoutesHandler) Close2FaRequest(c *gin.Context) { err := r.cqrs.Commands.Close2FaRequest.Handle(cmd) if err != nil { - var notFoundErr adapters.TokenRequestCouldNotBeFound + var notFoundErr adapters.TokenRequestCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) diff --git a/internal/api/browser_extension/service/service.go b/internal/api/browser_extension/service/service.go index 3978552..2f85e09 100644 --- a/internal/api/browser_extension/service/service.go +++ b/internal/api/browser_extension/service/service.go @@ -137,12 +137,10 @@ func (m *BrowserExtensionModule) RegisterPublicRoutes(router *gin.Engine) { publicRouter.GET("/browser_extensions/:extension_id/2fa_requests", m.RoutesHandler.GetAllBrowserExtension2FaTokenRequests) publicRouter.GET("/browser_extensions/:extension_id/2fa_requests/:token_request_id", m.RoutesHandler.GetBrowserExtension2FaTokenRequest) publicRouter.POST("/browser_extensions/:extension_id/2fa_requests/:token_request_id/commands/close_2fa_request", m.RoutesHandler.Close2FaRequest) - } func (m *BrowserExtensionModule) RegisterAdminRoutes(g *gin.RouterGroup) { if m.Config.IsTestingEnv() { g.DELETE("/browser_extensions", m.RoutesHandler.RemoveAllBrowserExtensions) g.DELETE("/browser_extensions/devices", m.RoutesHandler.RemoveAllBrowserExtensionsDevices) } - } diff --git a/internal/api/health/ports/http.go b/internal/api/health/ports/http.go index 8a517bd..66db45f 100644 --- a/internal/api/health/ports/http.go +++ b/internal/api/health/ports/http.go @@ -52,7 +52,12 @@ func (r *RoutesHandler) FakeError(c *gin.Context) { Message: "Fake error with message as JSON", } - message, _ := json.Marshal(messageStruct) + message, err := json.Marshal(messageStruct) + if err != nil { + logging.Error("Failed to marshal fake error message to JSON") + c.JSON(500, gin.H{}) + return + } logging.Error(string(message)) diff --git a/internal/api/health/service/service.go b/internal/api/health/service/service.go index c03a6df..363359e 100644 --- a/internal/api/health/service/service.go +++ b/internal/api/health/service/service.go @@ -3,6 +3,7 @@ package service import ( "github.com/gin-gonic/gin" "github.com/redis/go-redis/v9" + "github.com/twofas/2fas-server/config" "github.com/twofas/2fas-server/internal/api/health/ports" ) diff --git a/internal/api/icons/adapters/icons_collection_gorm_repository.go b/internal/api/icons/adapters/icons_collection_gorm_repository.go index ca19207..f811eb7 100644 --- a/internal/api/icons/adapters/icons_collection_gorm_repository.go +++ b/internal/api/icons/adapters/icons_collection_gorm_repository.go @@ -3,16 +3,18 @@ package adapters import ( "errors" "fmt" + "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/icons/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/icons/domain" ) -type IconsCollectionCouldNotBeFound struct { +type IconsCollectionCouldNotBeFoundError struct { IconsCollectionId string } -func (e IconsCollectionCouldNotBeFound) Error() string { +func (e IconsCollectionCouldNotBeFoundError) Error() string { return fmt.Sprintf("Icons collection could not be found: %s", e.IconsCollectionId) } @@ -54,7 +56,7 @@ func (r *IconsCollectionMysqlRepository) FindById(id uuid.UUID) (*domain.IconsCo result := r.db.First(&IconsCollection, "id = ?", id.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, IconsCollectionCouldNotBeFound{IconsCollectionId: id.String()} + return nil, IconsCollectionCouldNotBeFoundError{IconsCollectionId: id.String()} } return IconsCollection, nil diff --git a/internal/api/icons/adapters/icons_collections_relations_gorm_repository.go b/internal/api/icons/adapters/icons_collections_relations_gorm_repository.go index 17a2783..9c52446 100644 --- a/internal/api/icons/adapters/icons_collections_relations_gorm_repository.go +++ b/internal/api/icons/adapters/icons_collections_relations_gorm_repository.go @@ -2,8 +2,10 @@ package adapters import ( "fmt" - "github.com/twofas/2fas-server/internal/api/icons/domain" + "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/icons/domain" ) type IconsCollectionsRelationsMysqlRepository struct { diff --git a/internal/api/icons/adapters/icons_gorm_repository.go b/internal/api/icons/adapters/icons_gorm_repository.go index 961401e..526bbb6 100644 --- a/internal/api/icons/adapters/icons_gorm_repository.go +++ b/internal/api/icons/adapters/icons_gorm_repository.go @@ -11,11 +11,11 @@ import ( "github.com/twofas/2fas-server/internal/common/db" ) -type IconCouldNotBeFound struct { +type IconCouldNotBeFoundError struct { IconId string } -func (e IconCouldNotBeFound) Error() string { +func (e IconCouldNotBeFoundError) Error() string { return fmt.Sprintf("Icon could not be found: %s", e.IconId) } @@ -58,7 +58,7 @@ func (r *IconMysqlRepository) FindById(id uuid.UUID) (*domain.Icon, error) { if err := result.Error; err != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, IconCouldNotBeFound{IconId: id.String()} + return nil, IconCouldNotBeFoundError{IconId: id.String()} } return nil, db.WrapError(err) } diff --git a/internal/api/icons/adapters/icons_relations_gorm_repository.go b/internal/api/icons/adapters/icons_relations_gorm_repository.go index 70dec89..fa70d4b 100644 --- a/internal/api/icons/adapters/icons_relations_gorm_repository.go +++ b/internal/api/icons/adapters/icons_relations_gorm_repository.go @@ -2,8 +2,10 @@ package adapters import ( "fmt" - "github.com/twofas/2fas-server/internal/api/icons/domain" + "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/icons/domain" ) type IconsRelationsMysqlRepository struct { diff --git a/internal/api/icons/adapters/icons_requests_gorm_repository.go b/internal/api/icons/adapters/icons_requests_gorm_repository.go index 87912ab..3b5d532 100644 --- a/internal/api/icons/adapters/icons_requests_gorm_repository.go +++ b/internal/api/icons/adapters/icons_requests_gorm_repository.go @@ -11,11 +11,11 @@ import ( "github.com/twofas/2fas-server/internal/common/db" ) -type IconRequestCouldNotBeFound struct { +type IconRequestCouldNotBeFoundError struct { IconRequestId string } -func (e IconRequestCouldNotBeFound) Error() string { +func (e IconRequestCouldNotBeFoundError) Error() string { return fmt.Sprintf("Icon request could not be found: %s", e.IconRequestId) } @@ -57,7 +57,7 @@ func (r *IconRequestMysqlRepository) FindById(id uuid.UUID) (*domain.IconRequest result := r.db.First(&iconRequest, "id = ?", id.String()) if err := result.Error; err != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, IconRequestCouldNotBeFound{IconRequestId: id.String()} + return nil, IconRequestCouldNotBeFoundError{IconRequestId: id.String()} } return nil, db.WrapError(err) } diff --git a/internal/api/icons/adapters/mobile_web_services_gorm_repository.go b/internal/api/icons/adapters/mobile_web_services_gorm_repository.go index 55a1493..33fc246 100644 --- a/internal/api/icons/adapters/mobile_web_services_gorm_repository.go +++ b/internal/api/icons/adapters/mobile_web_services_gorm_repository.go @@ -11,11 +11,11 @@ import ( "github.com/twofas/2fas-server/internal/common/db" ) -type WebServiceCouldNotBeFound struct { +type WebServiceCouldNotBeFoundError struct { Identifier string } -func (e WebServiceCouldNotBeFound) Error() string { +func (e WebServiceCouldNotBeFoundError) Error() string { return fmt.Sprintf("Web service could not be found: %s", e.Identifier) } @@ -58,7 +58,7 @@ func (r *WebServiceMysqlRepository) FindById(id uuid.UUID) (*domain.WebService, if err := result.Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, WebServiceCouldNotBeFound{Identifier: id.String()} + return nil, WebServiceCouldNotBeFoundError{Identifier: id.String()} } return nil, db.WrapError(err) } @@ -73,7 +73,7 @@ func (r *WebServiceMysqlRepository) FindByName(name string) (*domain.WebService, if err := result.Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, WebServiceCouldNotBeFound{Identifier: name} + return nil, WebServiceCouldNotBeFoundError{Identifier: name} } return nil, db.WrapError(err) } diff --git a/internal/api/icons/app/command/icons_collection.go b/internal/api/icons/app/command/icons_collection.go index 6383f76..bca7285 100644 --- a/internal/api/icons/app/command/icons_collection.go +++ b/internal/api/icons/app/command/icons_collection.go @@ -2,11 +2,13 @@ package command import ( "encoding/json" + "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - domain2 "github.com/twofas/2fas-server/internal/api/icons/domain" "gorm.io/datatypes" "gorm.io/gorm" + + domain2 "github.com/twofas/2fas-server/internal/api/icons/domain" ) // CreateIconCollection diff --git a/internal/api/icons/app/command/icons_requests.go b/internal/api/icons/app/command/icons_requests.go index 58cad93..b5f6610 100644 --- a/internal/api/icons/app/command/icons_requests.go +++ b/internal/api/icons/app/command/icons_requests.go @@ -366,7 +366,7 @@ func (h *TransformIconRequestToWebServiceHandler) Handle(cmd *TransformIconReque if err == nil { return domain.WebServiceAlreadyExistsError{Name: iconRequest.ServiceName} } else { - var notFound adapters.WebServiceCouldNotBeFound + var notFound adapters.WebServiceCouldNotBeFoundError if !errors.As(err, ¬Found) { fmt.Printf("Error is: %T %+v\n", err, err) return fmt.Errorf("failed to find web service by name: %w", err) diff --git a/internal/api/icons/app/command/web_service.go b/internal/api/icons/app/command/web_service.go index a918511..80fe94a 100644 --- a/internal/api/icons/app/command/web_service.go +++ b/internal/api/icons/app/command/web_service.go @@ -56,7 +56,7 @@ func (h *CreateWebServiceHandler) Handle(cmd *CreateWebService) error { conflict, err := h.Repository.FindByName(cmd.Name) if err != nil { - var notFound adapters.WebServiceCouldNotBeFound + var notFound adapters.WebServiceCouldNotBeFoundError if !errors.As(err, ¬Found) { return err } diff --git a/internal/api/icons/app/queries/icon.go b/internal/api/icons/app/queries/icon.go index 4537aeb..b2594c7 100644 --- a/internal/api/icons/app/queries/icon.go +++ b/internal/api/icons/app/queries/icon.go @@ -2,8 +2,9 @@ package queries import ( "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/api/icons/adapters" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/icons/adapters" ) type IconPresenter struct { @@ -37,7 +38,7 @@ func (h *IconQueryHandler) FindOne(query *IconQuery) (*IconPresenter, error) { result := h.Database.Raw(sql).First(&presenter) if result.Error != nil { - return nil, adapters.IconCouldNotBeFound{IconId: query.Id} + return nil, adapters.IconCouldNotBeFoundError{IconId: query.Id} } return presenter, nil diff --git a/internal/api/icons/app/queries/icon_request.go b/internal/api/icons/app/queries/icon_request.go index a5a1f0d..3c864e6 100644 --- a/internal/api/icons/app/queries/icon_request.go +++ b/internal/api/icons/app/queries/icon_request.go @@ -2,9 +2,10 @@ package queries import ( "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/api/icons/adapters" "gorm.io/datatypes" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/icons/adapters" ) type IconRequestPresenter struct { @@ -56,7 +57,7 @@ func (h *IconRequestQueryHandler) FindOne(query *IconRequestQuery) (*IconRequest result := h.Database.Raw(sql).First(presenter) if result.Error != nil { - return nil, adapters.IconRequestCouldNotBeFound{IconRequestId: query.Id} + return nil, adapters.IconRequestCouldNotBeFoundError{IconRequestId: query.Id} } return presenter, nil diff --git a/internal/api/icons/app/queries/icons_collection.go b/internal/api/icons/app/queries/icons_collection.go index 98f0ce5..9a28eff 100644 --- a/internal/api/icons/app/queries/icons_collection.go +++ b/internal/api/icons/app/queries/icons_collection.go @@ -2,9 +2,10 @@ package queries import ( "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/api/icons/adapters" "gorm.io/datatypes" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/icons/adapters" ) type IconsCollectionPresenter struct { @@ -36,7 +37,7 @@ func (h *IconsCollectionQueryHandler) FindOne(query *IconsCollectionQuery) (*Ico result := h.Database.Raw(sql).First(&presenter) if result.Error != nil { - return nil, adapters.IconsCollectionCouldNotBeFound{IconsCollectionId: query.Id} + return nil, adapters.IconsCollectionCouldNotBeFoundError{IconsCollectionId: query.Id} } return presenter, nil diff --git a/internal/api/icons/app/queries/web_service.go b/internal/api/icons/app/queries/web_service.go index 3e45967..800718c 100644 --- a/internal/api/icons/app/queries/web_service.go +++ b/internal/api/icons/app/queries/web_service.go @@ -51,7 +51,7 @@ func (h *WebServiceQueryHandler) FindOne(query *WebServiceQuery) (*WebServicePre result := h.Database.Raw(sql).First(&presenter) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, adapters.WebServiceCouldNotBeFound{Identifier: query.Id} + return nil, adapters.WebServiceCouldNotBeFoundError{Identifier: query.Id} } return nil, db.WrapError(err) } diff --git a/internal/api/icons/app/queries/web_service_dump.go b/internal/api/icons/app/queries/web_service_dump.go index 82cb60a..07bbe2d 100644 --- a/internal/api/icons/app/queries/web_service_dump.go +++ b/internal/api/icons/app/queries/web_service_dump.go @@ -3,6 +3,8 @@ package queries import ( "database/sql" "encoding/json" + "fmt" + "gorm.io/datatypes" ) @@ -49,7 +51,10 @@ func (h *WebServicesDumpQueryHandler) Dump(q *WebServicesDumpQuery) ([]json.RawM return emptyResult, err } - out, _ := json.Marshal(&jsonRow) + out, err := json.Marshal(&jsonRow) + if err != nil { + return emptyResult, fmt.Errorf("could not marshal json: %w", err) + } services = append(services, out) } diff --git a/internal/api/icons/domain/icon.go b/internal/api/icons/domain/icon.go index ce83458..3466007 100644 --- a/internal/api/icons/domain/icon.go +++ b/internal/api/icons/domain/icon.go @@ -7,7 +7,7 @@ import ( const ( Light string = "light" - Dark = "dark" + Dark string = "dark" ) type Icon struct { diff --git a/internal/api/icons/domain/web_service.go b/internal/api/icons/domain/web_service.go index ed91541..df04d0c 100644 --- a/internal/api/icons/domain/web_service.go +++ b/internal/api/icons/domain/web_service.go @@ -2,6 +2,7 @@ package domain import ( "fmt" + "github.com/google/uuid" "gorm.io/datatypes" "gorm.io/gorm" diff --git a/internal/api/icons/ports/http.go b/internal/api/icons/ports/http.go index 03feccd..6f3b115 100644 --- a/internal/api/icons/ports/http.go +++ b/internal/api/icons/ports/http.go @@ -154,7 +154,7 @@ func (r *RoutesHandler) RemoveWebService(c *gin.Context) { err := r.cqrs.Commands.RemoveWebService.Handle(cmd) if err != nil { - var notFoundErr adapters.WebServiceCouldNotBeFound + var notFoundErr adapters.WebServiceCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -192,7 +192,7 @@ func (r *RoutesHandler) FindWebService(c *gin.Context) { result, err := r.cqrs.Queries.WebServiceQuery.FindOne(q) if err != nil { - var notFoundErr adapters.WebServiceCouldNotBeFound + var notFoundErr adapters.WebServiceCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -358,7 +358,7 @@ func (r *RoutesHandler) RemoveIcon(c *gin.Context) { err := r.cqrs.Commands.RemoveIcon.Handle(cmd) if err != nil { - var notFoundErr adapters.IconCouldNotBeFound + var notFoundErr adapters.IconCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -396,7 +396,7 @@ func (r *RoutesHandler) FindIcon(c *gin.Context) { result, err := r.cqrs.Queries.IconQuery.FindOne(q) if err != nil { - var notFoundErr adapters.IconCouldNotBeFound + var notFoundErr adapters.IconCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -494,7 +494,7 @@ func (r *RoutesHandler) RemoveIconRequest(c *gin.Context) { err := r.cqrs.Commands.RemoveIconRequest.Handle(cmd) if err != nil { - var notFoundErr adapters.IconRequestCouldNotBeFound + var notFoundErr adapters.IconRequestCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -622,7 +622,7 @@ func (r *RoutesHandler) FindIconRequest(c *gin.Context) { result, err := r.cqrs.Queries.IconRequestQuery.FindOne(q) if err != nil { - var notFoundErr adapters.IconRequestCouldNotBeFound + var notFoundErr adapters.IconRequestCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -756,7 +756,7 @@ func (r *RoutesHandler) RemoveIconsCollection(c *gin.Context) { err := r.cqrs.Commands.RemoveIconsCollection.Handle(cmd) if err != nil { - var notFoundErr adapters.IconsCollectionCouldNotBeFound + var notFoundErr adapters.IconsCollectionCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -786,7 +786,7 @@ func (r *RoutesHandler) FindIconsCollection(c *gin.Context) { result, err := r.cqrs.Queries.IconsCollectionQuery.FindOne(q) if err != nil { - var notFoundErr adapters.IconsCollectionCouldNotBeFound + var notFoundErr adapters.IconsCollectionCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) diff --git a/internal/api/mobile/adapters/mobile_application_extensions_storage.go b/internal/api/mobile/adapters/mobile_application_extensions_storage.go index e0e72fe..176493c 100644 --- a/internal/api/mobile/adapters/mobile_application_extensions_storage.go +++ b/internal/api/mobile/adapters/mobile_application_extensions_storage.go @@ -3,8 +3,9 @@ package adapters import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/common/clock" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/common/clock" ) type DeviceExtensionsService struct { diff --git a/internal/api/mobile/adapters/mobile_device_extensions_gorm_repository.go b/internal/api/mobile/adapters/mobile_device_extensions_gorm_repository.go index a99f0bf..3a07d58 100644 --- a/internal/api/mobile/adapters/mobile_device_extensions_gorm_repository.go +++ b/internal/api/mobile/adapters/mobile_device_extensions_gorm_repository.go @@ -3,18 +3,20 @@ package adapters import ( "errors" "fmt" + "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/mobile/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) -type MobileDeviceExtensionCouldNotBeFound struct { +type MobileDeviceExtensionCouldNotBeFoundError struct { DeviceId string ExtensionId string } -func (e MobileDeviceExtensionCouldNotBeFound) Error() string { +func (e MobileDeviceExtensionCouldNotBeFoundError) Error() string { return fmt.Sprintf("Mobile device could not be found: %s", e.DeviceId) } @@ -36,7 +38,7 @@ func (r *MobileDeviceExtensionsGormRepository) FindById(deviceId, extensionId uu result := r.db.First(&pairedExtension, "device_id = ? and extension_id = ?", deviceId.String(), extensionId.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, MobileDeviceExtensionCouldNotBeFound{DeviceId: deviceId.String(), ExtensionId: extensionId.String()} + return nil, MobileDeviceExtensionCouldNotBeFoundError{DeviceId: deviceId.String(), ExtensionId: extensionId.String()} } return pairedExtension, nil diff --git a/internal/api/mobile/adapters/mobile_device_gorm_repository.go b/internal/api/mobile/adapters/mobile_device_gorm_repository.go index 197937b..cccf380 100644 --- a/internal/api/mobile/adapters/mobile_device_gorm_repository.go +++ b/internal/api/mobile/adapters/mobile_device_gorm_repository.go @@ -3,16 +3,18 @@ package adapters import ( "errors" "fmt" + "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/mobile/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) -type MobileDeviceCouldNotBeFound struct { +type MobileDeviceCouldNotBeFoundError struct { DeviceId string } -func (e MobileDeviceCouldNotBeFound) Error() string { +func (e MobileDeviceCouldNotBeFoundError) Error() string { return fmt.Sprintf("Mobile device could not be found: %s", e.DeviceId) } @@ -46,7 +48,7 @@ func (r *MobileDeviceMysqlRepository) FindById(id uuid.UUID) (*domain.MobileDevi result := r.db.First(&device, "id = ?", id.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, MobileDeviceCouldNotBeFound{DeviceId: id.String()} + return nil, MobileDeviceCouldNotBeFoundError{DeviceId: id.String()} } return device, nil diff --git a/internal/api/mobile/adapters/mobile_notifications_gorm_repository.go b/internal/api/mobile/adapters/mobile_notifications_gorm_repository.go index 2837f58..2c2134b 100644 --- a/internal/api/mobile/adapters/mobile_notifications_gorm_repository.go +++ b/internal/api/mobile/adapters/mobile_notifications_gorm_repository.go @@ -3,16 +3,18 @@ package adapters import ( "errors" "fmt" + "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/mobile/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) -type MobileNotificationCouldNotBeFound struct { +type MobileNotificationCouldNotBeFoundError struct { NotificationId string } -func (e MobileNotificationCouldNotBeFound) Error() string { +func (e MobileNotificationCouldNotBeFoundError) Error() string { return fmt.Sprintf("Mobile notification could not be found: %s", e.NotificationId) } @@ -54,7 +56,7 @@ func (r *MobileNotificationMysqlRepository) FindById(id uuid.UUID) (*domain.Mobi result := r.db.First(¬ification, "id = ?", id.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, MobileNotificationCouldNotBeFound{NotificationId: id.String()} + return nil, MobileNotificationCouldNotBeFoundError{NotificationId: id.String()} } return notification, nil diff --git a/internal/api/mobile/app/app.go b/internal/api/mobile/app/app.go index b35c23f..f6f5e32 100644 --- a/internal/api/mobile/app/app.go +++ b/internal/api/mobile/app/app.go @@ -2,7 +2,7 @@ package app import ( "github.com/twofas/2fas-server/internal/api/mobile/app/command" - "github.com/twofas/2fas-server/internal/api/mobile/app/queries" + query "github.com/twofas/2fas-server/internal/api/mobile/app/queries" ) type Commands struct { diff --git a/internal/api/mobile/app/command/create_whatsnew_notification.go b/internal/api/mobile/app/command/create_whatsnew_notification.go index a538885..4074a6a 100644 --- a/internal/api/mobile/app/command/create_whatsnew_notification.go +++ b/internal/api/mobile/app/command/create_whatsnew_notification.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) diff --git a/internal/api/mobile/app/command/delete_whatsnew_notification.go b/internal/api/mobile/app/command/delete_whatsnew_notification.go index 951c8d0..8da04bb 100644 --- a/internal/api/mobile/app/command/delete_whatsnew_notification.go +++ b/internal/api/mobile/app/command/delete_whatsnew_notification.go @@ -3,8 +3,9 @@ package command import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/mobile/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) type DeleteNotification struct { diff --git a/internal/api/mobile/app/command/publish_whatsnew_notification.go b/internal/api/mobile/app/command/publish_whatsnew_notification.go index 67b6228..8ca80bc 100644 --- a/internal/api/mobile/app/command/publish_whatsnew_notification.go +++ b/internal/api/mobile/app/command/publish_whatsnew_notification.go @@ -2,9 +2,11 @@ package command import ( "database/sql" + "time" + "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" - "time" ) type PublishNotification struct { diff --git a/internal/api/mobile/app/command/register_mobile_device.go b/internal/api/mobile/app/command/register_mobile_device.go index 3943b21..e8201f5 100644 --- a/internal/api/mobile/app/command/register_mobile_device.go +++ b/internal/api/mobile/app/command/register_mobile_device.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) diff --git a/internal/api/mobile/app/command/remove_device_extension.go b/internal/api/mobile/app/command/remove_device_extension.go index 3b9a29a..45b5343 100644 --- a/internal/api/mobile/app/command/remove_device_extension.go +++ b/internal/api/mobile/app/command/remove_device_extension.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) diff --git a/internal/api/mobile/app/command/update_mobile_device.go b/internal/api/mobile/app/command/update_mobile_device.go index 42d2776..72e1076 100644 --- a/internal/api/mobile/app/command/update_mobile_device.go +++ b/internal/api/mobile/app/command/update_mobile_device.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) diff --git a/internal/api/mobile/app/command/update_whatsnew_notification.go b/internal/api/mobile/app/command/update_whatsnew_notification.go index 8c333b9..cbad298 100644 --- a/internal/api/mobile/app/command/update_whatsnew_notification.go +++ b/internal/api/mobile/app/command/update_whatsnew_notification.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) diff --git a/internal/api/mobile/app/queries/device_browser_extensions_2fa_requests.go b/internal/api/mobile/app/queries/device_browser_extensions_2fa_requests.go index 5baad41..959a76e 100644 --- a/internal/api/mobile/app/queries/device_browser_extensions_2fa_requests.go +++ b/internal/api/mobile/app/queries/device_browser_extensions_2fa_requests.go @@ -1,10 +1,12 @@ package query import ( + "time" + "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/common/clock" "gorm.io/gorm" - "time" + + "github.com/twofas/2fas-server/internal/common/clock" ) type DeviceBrowserExtension2FaRequestPresenter struct { diff --git a/internal/api/mobile/app/queries/mobile_device.go b/internal/api/mobile/app/queries/mobile_device.go index 6f60b3a..5fb7025 100644 --- a/internal/api/mobile/app/queries/mobile_device.go +++ b/internal/api/mobile/app/queries/mobile_device.go @@ -7,7 +7,7 @@ import ( ) var ( - MobileDeviceNotFound = errors.New("Mobile device can not be found") + ErrMobileDeviceNotFound = errors.New("Mobile device can not be found") ) type MobileDevicePresenter struct { @@ -37,7 +37,7 @@ func (h *MobileDeviceQueryHandler) Handle(query *MobileDeviceQuery) (*MobileDevi result := h.Database.Raw(sql).Scan(&presenter) if result.Error != nil { - return nil, MobileDeviceNotFound + return nil, ErrMobileDeviceNotFound } return presenter, nil diff --git a/internal/api/mobile/app/queries/whatsnew_notification.go b/internal/api/mobile/app/queries/whatsnew_notification.go index e530a0a..05abeff 100644 --- a/internal/api/mobile/app/queries/whatsnew_notification.go +++ b/internal/api/mobile/app/queries/whatsnew_notification.go @@ -2,8 +2,9 @@ package query import ( "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/api/mobile/adapters" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/mobile/adapters" ) type MobileNotificationPresenter struct { @@ -43,7 +44,7 @@ func (h *MobileNotificationsQueryHandler) FindOne(query *MobileNotificationsQuer result := h.Database.Raw(sql).First(&presenter) if result.Error != nil { - return nil, adapters.MobileNotificationCouldNotBeFound{NotificationId: query.Id} + return nil, adapters.MobileNotificationCouldNotBeFoundError{NotificationId: query.Id} } return presenter, nil diff --git a/internal/api/mobile/app/security/middleware.go b/internal/api/mobile/app/security/middleware.go index ccf589d..eae0d70 100644 --- a/internal/api/mobile/app/security/middleware.go +++ b/internal/api/mobile/app/security/middleware.go @@ -7,6 +7,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/twofas/2fas-server/internal/common/logging" "github.com/twofas/2fas-server/internal/common/rate_limit" ) diff --git a/internal/api/mobile/domain/device_browser_extension.go b/internal/api/mobile/domain/device_browser_extension.go index a4e8361..2bbd6bd 100644 --- a/internal/api/mobile/domain/device_browser_extension.go +++ b/internal/api/mobile/domain/device_browser_extension.go @@ -2,6 +2,7 @@ package domain import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/browser_extension/domain" ) diff --git a/internal/api/mobile/domain/whatsnew_notification.go b/internal/api/mobile/domain/whatsnew_notification.go index db95278..f4cdc44 100644 --- a/internal/api/mobile/domain/whatsnew_notification.go +++ b/internal/api/mobile/domain/whatsnew_notification.go @@ -2,6 +2,7 @@ package domain import ( "database/sql" + "github.com/google/uuid" "gorm.io/gorm" ) diff --git a/internal/api/mobile/ports/http.go b/internal/api/mobile/ports/http.go index 4d6af46..21512e1 100644 --- a/internal/api/mobile/ports/http.go +++ b/internal/api/mobile/ports/http.go @@ -58,7 +58,7 @@ func (r *RoutesHandler) UpdateMobileDevice(c *gin.Context) { err := r.cqrs.Commands.UpdateMobileDevice.Handle(cmd) if err != nil { - var deviceNotFoundErr adapters.MobileDeviceCouldNotBeFound + var deviceNotFoundErr adapters.MobileDeviceCouldNotBeFoundError if errors.As(err, &deviceNotFoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -192,8 +192,8 @@ func (r *RoutesHandler) RemovePairingWithExtension(c *gin.Context) { err := r.cqrs.Commands.RemovePairingWithExtension.Handle(cmd) if err != nil { - var deviceNotFoundErr *adapters.MobileDeviceCouldNotBeFound - var extensionsNotFoundErr *browser_adapters.BrowserExtensionsCouldNotBeFound + var deviceNotFoundErr *adapters.MobileDeviceCouldNotBeFoundError + var extensionsNotFoundErr *browser_adapters.BrowserExtensionsCouldNotBeFoundError if errors.As(err, &deviceNotFoundErr) || errors.As(err, &extensionsNotFoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -262,7 +262,7 @@ func (r *RoutesHandler) FindMobileAppExtensionById(c *gin.Context) { result, err := r.cqrs.Queries.DeviceBrowserExtensionsQuery.Handle(cmd) if len(result) == 0 { - c.JSON(404, api.NotFoundError(browser_adapters.BrowserExtensionsCouldNotBeFound{ExtensionId: cmd.ExtensionId})) + c.JSON(404, api.NotFoundError(browser_adapters.BrowserExtensionsCouldNotBeFoundError{ExtensionId: cmd.ExtensionId})) return } @@ -394,7 +394,7 @@ func (r *RoutesHandler) UpdateMobileNotification(c *gin.Context) { err := r.cqrs.Commands.UpdateNotification.Handle(cmd) if err != nil { - var notificationNotFoundErr *adapters.MobileNotificationCouldNotBeFound + var notificationNotFoundErr *adapters.MobileNotificationCouldNotBeFoundError if errors.As(err, ¬ificationNotFoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -461,7 +461,7 @@ func (r *RoutesHandler) FindMobileNotification(c *gin.Context) { result, err := r.cqrs.Queries.MobileNotificationsQuery.FindOne(q) if err != nil { - var notificationNotFoundErr adapters.MobileNotificationCouldNotBeFound + var notificationNotFoundErr adapters.MobileNotificationCouldNotBeFoundError if errors.As(err, ¬ificationNotFoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -491,7 +491,7 @@ func (r *RoutesHandler) RemoveMobileNotification(c *gin.Context) { err := r.cqrs.Commands.DeleteNotification.Handle(cmd) if err != nil { - var notFoundErr adapters.MobileNotificationCouldNotBeFound + var notFoundErr adapters.MobileNotificationCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -529,7 +529,7 @@ func (r *RoutesHandler) PublishMobileNotification(c *gin.Context) { err := r.cqrs.Commands.PublishNotification.Handle(cmd) if err != nil { - var notificationNotFoundErr adapters.MobileNotificationCouldNotBeFound + var notificationNotFoundErr adapters.MobileNotificationCouldNotBeFoundError if errors.As(err, ¬ificationNotFoundErr) { c.JSON(404, api.NotFoundError(err)) diff --git a/internal/api/mobile/service/validation.go b/internal/api/mobile/service/validation.go index 7724eaa..fac1031 100644 --- a/internal/api/mobile/service/validation.go +++ b/internal/api/mobile/service/validation.go @@ -3,6 +3,7 @@ package service import ( "github.com/go-playground/validator/v10" "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/mobile/domain" ) diff --git a/internal/api/support/adapters/debug_logs_audit_gorm_repository.go b/internal/api/support/adapters/debug_logs_audit_gorm_repository.go index b8713da..b3a13e3 100644 --- a/internal/api/support/adapters/debug_logs_audit_gorm_repository.go +++ b/internal/api/support/adapters/debug_logs_audit_gorm_repository.go @@ -3,16 +3,18 @@ package adapters import ( "errors" "fmt" + "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/support/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/support/domain" ) -type DebugLogsAuditCouldNotBeFound struct { +type DebugLogsAuditCouldNotBeFoundError struct { AuditId string } -func (e DebugLogsAuditCouldNotBeFound) Error() string { +func (e DebugLogsAuditCouldNotBeFoundError) Error() string { return fmt.Sprintf("Debug logs audit could not be found: %s", e.AuditId) } @@ -46,7 +48,7 @@ func (r *DebugLogsAuditMysqlRepository) FindById(id uuid.UUID) (*domain.DebugLog result := r.db.First(&audit, "id = ?", id.String()) if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, DebugLogsAuditCouldNotBeFound{AuditId: id.String()} + return nil, DebugLogsAuditCouldNotBeFoundError{AuditId: id.String()} } return audit, nil diff --git a/internal/api/support/app/command/create_debug_log_audit.go b/internal/api/support/app/command/create_debug_log_audit.go index 9b1a3e4..3fcc9c4 100644 --- a/internal/api/support/app/command/create_debug_log_audit.go +++ b/internal/api/support/app/command/create_debug_log_audit.go @@ -31,7 +31,7 @@ func (h *CreateDebugLogsAuditHandler) Handle(command *CreateDebugLogsAudit) erro id, err := uuid.Parse(command.Id) if err != nil { - return adapters.DebugLogsAuditCouldNotBeFound{AuditId: id.String()} + return adapters.DebugLogsAuditCouldNotBeFoundError{AuditId: id.String()} } auditClaim, err := h.DebugLogsAuditRepository.FindById(id) diff --git a/internal/api/support/app/command/create_debug_log_audit_claim.go b/internal/api/support/app/command/create_debug_log_audit_claim.go index 3f7bb65..b25fea3 100644 --- a/internal/api/support/app/command/create_debug_log_audit_claim.go +++ b/internal/api/support/app/command/create_debug_log_audit_claim.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/support/domain" "github.com/twofas/2fas-server/internal/common/clock" ) diff --git a/internal/api/support/app/command/delete_debug_logs_audit.go b/internal/api/support/app/command/delete_debug_logs_audit.go index ac21a8a..7129454 100644 --- a/internal/api/support/app/command/delete_debug_logs_audit.go +++ b/internal/api/support/app/command/delete_debug_logs_audit.go @@ -3,8 +3,9 @@ package command import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - "github.com/twofas/2fas-server/internal/api/support/domain" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/support/domain" ) type DeleteDebugLogsAudit struct { diff --git a/internal/api/support/app/command/update_debug_log_audit.go b/internal/api/support/app/command/update_debug_log_audit.go index a9ebc90..a26218f 100644 --- a/internal/api/support/app/command/update_debug_log_audit.go +++ b/internal/api/support/app/command/update_debug_log_audit.go @@ -2,6 +2,7 @@ package command import ( "github.com/google/uuid" + "github.com/twofas/2fas-server/internal/api/support/domain" ) diff --git a/internal/api/support/app/queries/debug_logs_audit.go b/internal/api/support/app/queries/debug_logs_audit.go index 5864753..16dedb5 100644 --- a/internal/api/support/app/queries/debug_logs_audit.go +++ b/internal/api/support/app/queries/debug_logs_audit.go @@ -2,8 +2,9 @@ package queries import ( "github.com/doug-martin/goqu/v9" - "github.com/twofas/2fas-server/internal/api/support/adapters" "gorm.io/gorm" + + "github.com/twofas/2fas-server/internal/api/support/adapters" ) type DebugLogsAuditPresenter struct { @@ -35,7 +36,7 @@ func (h *DebugLogsAuditQueryHandler) Find(query *DebugLogsAuditQuery) (*DebugLog result := h.Database.Raw(sql).First(&presenter) if result.Error != nil { - return nil, adapters.DebugLogsAuditCouldNotBeFound{AuditId: query.Id} + return nil, adapters.DebugLogsAuditCouldNotBeFoundError{AuditId: query.Id} } return presenter, nil diff --git a/internal/api/support/domain/debug_logs_audit.go b/internal/api/support/domain/debug_logs_audit.go index 2c56662..3504ba1 100644 --- a/internal/api/support/domain/debug_logs_audit.go +++ b/internal/api/support/domain/debug_logs_audit.go @@ -1,9 +1,10 @@ package domain import ( + "time" + "github.com/google/uuid" "gorm.io/gorm" - "time" ) type DebugLogsAudit struct { diff --git a/internal/api/support/ports/http.go b/internal/api/support/ports/http.go index be394f3..0773fea 100644 --- a/internal/api/support/ports/http.go +++ b/internal/api/support/ports/http.go @@ -98,7 +98,7 @@ func (r *RoutesHandler) CreateDebugLogsAudit(c *gin.Context) { err := r.cqrs.Commands.CreateDebugLogsAudit.Handle(cmd) if err != nil { - var notFoundErr adapters3.DebugLogsAuditCouldNotBeFound + var notFoundErr adapters3.DebugLogsAuditCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -159,7 +159,7 @@ func (r *RoutesHandler) UpdateDebugLogsAuditClaim(c *gin.Context) { err := r.cqrs.Commands.UpdateDebugLogsAudit.Handle(cmd) if err != nil { - var notFoundErr adapters3.DebugLogsAuditCouldNotBeFound + var notFoundErr adapters3.DebugLogsAuditCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -200,7 +200,7 @@ func (r *RoutesHandler) DeleteDebugLogsAudit(c *gin.Context) { err := r.cqrs.Commands.DeleteDebugLogsAudit.Handle(cmd) if err != nil { - var notFoundErr adapters3.DebugLogsAuditCouldNotBeFound + var notFoundErr adapters3.DebugLogsAuditCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) @@ -233,7 +233,7 @@ func (r *RoutesHandler) GetDebugLogsAudit(c *gin.Context) { presenter, err := r.cqrs.Queries.DebugLogsAuditQuery.Find(q) if err != nil { - var notFoundErr adapters3.DebugLogsAuditCouldNotBeFound + var notFoundErr adapters3.DebugLogsAuditCouldNotBeFoundError if errors.As(err, ¬FoundErr) { c.JSON(404, api.NotFoundError(err)) diff --git a/internal/common/crypto/rsa.go b/internal/common/crypto/rsa.go index 36b34dc..9a10ac0 100644 --- a/internal/common/crypto/rsa.go +++ b/internal/common/crypto/rsa.go @@ -7,6 +7,7 @@ import ( "crypto/x509" "encoding/base64" "encoding/pem" + "github.com/pkg/errors" ) diff --git a/internal/common/crypto/rsa_test.go b/internal/common/crypto/rsa_test.go index 178c30b..4a05f17 100644 --- a/internal/common/crypto/rsa_test.go +++ b/internal/common/crypto/rsa_test.go @@ -1,23 +1,26 @@ package crypto import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/require" ) func Test_ConvertKeyPairToStringAndBackward(t *testing.T) { + t.Helper() + keyPair := GenerateKeyPair(2048) privateKeyAsPemStr := ExportRsaPrivateKeyAsPemStr(keyPair.PrivateKey) publicKeyAsPemStr := ExportRsaPublicKeyAsPemStr(keyPair.PublicKey) - assert.NotEmpty(t, publicKeyAsPemStr) + require.NotEmpty(t, publicKeyAsPemStr) _, err := ParseRsaPrivateKeyFromPemStr(privateKeyAsPemStr) - assert.NoError(t, err, "Cannot convert PEM string to private key") + require.NoError(t, err, "Cannot convert PEM string to private key") _, err = ParseRsaPublicKeyFromPemStr(publicKeyAsPemStr) - assert.NoError(t, err) + require.NoError(t, err) } diff --git a/internal/common/db/db.go b/internal/common/db/db.go index 2b8c7bd..dc0d3f3 100644 --- a/internal/common/db/db.go +++ b/internal/common/db/db.go @@ -3,7 +3,9 @@ package db import ( "database/sql" "fmt" + "github.com/go-sql-driver/mysql" + "github.com/twofas/2fas-server/config" ) diff --git a/internal/common/db/error.go b/internal/common/db/error.go index e223a58..4bfc02c 100644 --- a/internal/common/db/error.go +++ b/internal/common/db/error.go @@ -5,7 +5,7 @@ import ( "fmt" ) -var dbError = errors.New("database error") +var dbError = errors.New("database error") //nolint:errname func WrapError(err error) error { return fmt.Errorf("%w: %w", dbError, err) diff --git a/internal/common/db/error_test.go b/internal/common/db/error_test.go index 868ab41..ecba77a 100644 --- a/internal/common/db/error_test.go +++ b/internal/common/db/error_test.go @@ -36,5 +36,4 @@ func TestIsDBError(t *testing.T) { } }) } - } diff --git a/internal/common/db/gorm.go b/internal/common/db/gorm.go index c7bf14c..2fffb54 100644 --- a/internal/common/db/gorm.go +++ b/internal/common/db/gorm.go @@ -3,9 +3,10 @@ package db import ( "fmt" - "github.com/twofas/2fas-server/config" "gorm.io/gorm/logger" + "github.com/twofas/2fas-server/config" + gosql "github.com/go-sql-driver/mysql" "gorm.io/driver/mysql" diff --git a/internal/common/db/query_builder.go b/internal/common/db/query_builder.go index e98d9b1..e272bfe 100644 --- a/internal/common/db/query_builder.go +++ b/internal/common/db/query_builder.go @@ -2,6 +2,7 @@ package db import ( "database/sql" + "github.com/doug-martin/goqu/v9" _ "github.com/doug-martin/goqu/v9/dialect/mysql" ) diff --git a/internal/common/http/cors.go b/internal/common/http/cors.go index 5cb9200..9bf7398 100644 --- a/internal/common/http/cors.go +++ b/internal/common/http/cors.go @@ -1,9 +1,10 @@ package http import ( + "time" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" - "time" ) func corsMiddleware() gin.HandlerFunc { diff --git a/internal/common/http/error.go b/internal/common/http/error.go index bf03184..7f57cf5 100644 --- a/internal/common/http/error.go +++ b/internal/common/http/error.go @@ -2,11 +2,11 @@ package http import "fmt" -type ErrorResponse struct { +type ResponseError struct { Status int Message string `json:"message"` } -func (error *ErrorResponse) Error() string { +func (error *ResponseError) Error() string { return fmt.Sprintf("Status: %d Message: %s", error.Status, error.Message) } diff --git a/internal/common/http/log.go b/internal/common/http/log.go index c6edadf..f5ad371 100644 --- a/internal/common/http/log.go +++ b/internal/common/http/log.go @@ -32,7 +32,6 @@ func LoggingMiddleware() gin.HandlerFunc { "request_id": requestId, })) c.Request = c.Request.WithContext(ctxWithLog) - } } diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index 79e3b5f..b91521e 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -95,7 +95,11 @@ func Fatalf(format string, args ...interface{}) { } func LogCommand(command interface{}) { - context, _ := json.Marshal(command) + context, err := json.Marshal(command) + if err != nil { + log.Errorf("Failed to marshal command for logging: %v", err) + // This will only break the command logging so we can continue. + } commandName := reflect.TypeOf(command).Elem().Name() diff --git a/internal/common/push/client.go b/internal/common/push/client.go index f7a2347..16f79a0 100644 --- a/internal/common/push/client.go +++ b/internal/common/push/client.go @@ -3,14 +3,16 @@ package push import ( "context" "encoding/json" + "fmt" "io/ioutil" "time" firebase "firebase.google.com/go/v4" "firebase.google.com/go/v4/messaging" + "google.golang.org/api/option" + "github.com/twofas/2fas-server/internal/api/mobile/domain" "github.com/twofas/2fas-server/internal/common/logging" - "google.golang.org/api/option" ) type Pusher interface { @@ -36,6 +38,9 @@ func NewFcmPushClient(config *domain.FcmPushConfig) *FcmPushClient { } client, err := app.Messaging(context.Background()) + if err != nil { + logging.WithField("error", err.Error()).Fatal("Error initializing FCM Messaging client") + } return &FcmPushClient{ FcmMessaging: client, @@ -43,7 +48,10 @@ func NewFcmPushClient(config *domain.FcmPushConfig) *FcmPushClient { } func (p *FcmPushClient) Send(ctx context.Context, message *messaging.Message) error { - data, _ := json.Marshal(message) + data, err := json.Marshal(message) + if err != nil { + return fmt.Errorf("could not marshal the message: %w", err) + } logging.WithFields(logging.Fields{ "notification": string(data), @@ -71,7 +79,10 @@ func NewFakePushClient() *FakePushClient { } func (p *FakePushClient) Send(ctx context.Context, message *messaging.Message) error { - data, _ := json.Marshal(message) + data, err := json.Marshal(message) + if err != nil { + return fmt.Errorf("could not marshal the message: %w", err) + } logging.WithFields(logging.Fields{ "notification": string(data), diff --git a/internal/common/rate_limit/redis_rate_limit.go b/internal/common/rate_limit/redis_rate_limit.go index bfe9036..374b3e1 100644 --- a/internal/common/rate_limit/redis_rate_limit.go +++ b/internal/common/rate_limit/redis_rate_limit.go @@ -6,6 +6,7 @@ import ( "github.com/go-redis/redis_rate/v10" "github.com/redis/go-redis/v9" + "github.com/twofas/2fas-server/internal/common/logging" ) diff --git a/internal/common/recovery/gin.go b/internal/common/recovery/gin.go index a2c8750..863be2e 100644 --- a/internal/common/recovery/gin.go +++ b/internal/common/recovery/gin.go @@ -7,6 +7,7 @@ import ( "runtime" "github.com/gin-gonic/gin" + "github.com/twofas/2fas-server/internal/common/logging" ) @@ -85,6 +86,6 @@ func function(pc uintptr) []byte { if period := bytes.Index(name, []byte(".")); period >= 0 { name = name[period+1:] } - name = bytes.Replace(name, []byte("·"), []byte("."), -1) + name = bytes.ReplaceAll(name, []byte("·"), []byte(".")) return name } diff --git a/internal/common/security/middleware.go b/internal/common/security/middleware.go index b1a35a4..113bf1f 100644 --- a/internal/common/security/middleware.go +++ b/internal/common/security/middleware.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/twofas/2fas-server/internal/common/logging" "github.com/twofas/2fas-server/internal/common/rate_limit" ) diff --git a/internal/common/tests/integration_test_suite.go b/internal/common/tests/integration_test_suite.go index 5e53a6f..7f3ef12 100644 --- a/internal/common/tests/integration_test_suite.go +++ b/internal/common/tests/integration_test_suite.go @@ -1,8 +1,9 @@ package tests import ( - "github.com/stretchr/testify/suite" "testing" + + "github.com/stretchr/testify/suite" ) type IntegrationTestSuite struct { diff --git a/internal/common/validation/validators.go b/internal/common/validation/validators.go index 4575266..c6d5bdc 100644 --- a/internal/common/validation/validators.go +++ b/internal/common/validation/validators.go @@ -1,8 +1,9 @@ package validation import ( - "github.com/go-playground/validator/v10" "strings" + + "github.com/go-playground/validator/v10" ) func NotBlank(fl validator.FieldLevel) bool { diff --git a/internal/websocket/app.go b/internal/websocket/app.go index f54c36a..95b161f 100644 --- a/internal/websocket/app.go +++ b/internal/websocket/app.go @@ -2,6 +2,7 @@ package websocket import ( "github.com/gin-gonic/gin" + "github.com/twofas/2fas-server/internal/common/http" "github.com/twofas/2fas-server/internal/common/recovery" "github.com/twofas/2fas-server/internal/websocket/browser_extension" diff --git a/internal/websocket/browser_extension/service.go b/internal/websocket/browser_extension/service.go index da06b94..66408c2 100644 --- a/internal/websocket/browser_extension/service.go +++ b/internal/websocket/browser_extension/service.go @@ -2,6 +2,7 @@ package browser_extension import ( "github.com/gin-gonic/gin" + "github.com/twofas/2fas-server/internal/websocket/common" ) diff --git a/internal/websocket/common/client.go b/internal/websocket/common/client.go index 41d8a1d..2c150df 100644 --- a/internal/websocket/common/client.go +++ b/internal/websocket/common/client.go @@ -85,7 +85,7 @@ func (c *Client) readPump(log logging.FieldLogger) { break } - message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1)) + message = bytes.TrimSpace(bytes.ReplaceAll(message, newline, space)) go c.hub.broadcastMsg(message) } diff --git a/internal/websocket/common/handler.go b/internal/websocket/common/handler.go index 965623b..470d6e3 100644 --- a/internal/websocket/common/handler.go +++ b/internal/websocket/common/handler.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "github.com/twofas/2fas-server/internal/common/logging" "github.com/twofas/2fas-server/internal/common/recovery" )