diff --git a/tests/browser_extension/browser_extension_2fa_request_test.go b/tests/browser_extension/browser_extension_2fa_request_test.go index dc44336..a7eb8ab 100644 --- a/tests/browser_extension/browser_extension_2fa_request_test.go +++ b/tests/browser_extension/browser_extension_2fa_request_test.go @@ -3,104 +3,119 @@ package tests import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" "github.com/twofas/2fas-server/tests" "testing" ) -func Test_Request2FaToken(t *testing.T) { - browserExtension := tests.CreateBrowserExtension(t, "go-ext") +func TestBrowserExtensionTwoFactorAuthTestSuite(t *testing.T) { + suite.Run(t, new(BrowserExtensionTwoFactorAuthTestSuite)) +} + +type BrowserExtensionTwoFactorAuthTestSuite struct { + suite.Suite +} + +func (s *BrowserExtensionTwoFactorAuthTestSuite) SetupTest() { + tests.DoSuccessDelete(s.T(), "/mobile/devices") + tests.DoSuccessDelete(s.T(), "/browser_extensions") + tests.DoSuccessDelete(s.T(), "browser_extensions/devices") +} + +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestRequest2FaToken() { + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") var tokenRequest *tests.AuthTokenRequestResponse request2FaTokenPayload := []byte(`{"domain":"https://facebook.com/path/nested"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", request2FaTokenPayload, &tokenRequest) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", request2FaTokenPayload, &tokenRequest) - assert.Equal(t, browserExtension.Id, tokenRequest.ExtensionId) + assert.Equal(s.T(), browserExtension.Id, tokenRequest.ExtensionId) var tokenRequestById *tests.AuthTokenRequestResponse - tests.DoSuccessGet(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &tokenRequestById) - assert.Equal(t, tokenRequest.Id, tokenRequestById.Id) - assert.Equal(t, "https://facebook.com", tokenRequestById.Domain) + tests.DoSuccessGet(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) } -func Test_FindAll2FaRequestsForBrowserExtension(t *testing.T) { - browserExtension := tests.CreateBrowserExtension(t, "go-ext") +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestFindAll2FaRequestsForBrowserExtension() { + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") facebook2FaTokenRequest := []byte(`{"domain":"facebook.com"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", facebook2FaTokenRequest, nil) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", facebook2FaTokenRequest, nil) google2FaTokenRequest := []byte(`{"domain":"google.com"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", google2FaTokenRequest, nil) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", google2FaTokenRequest, nil) var tokenRequestsCollection []*tests.AuthTokenRequestResponse - tests.DoSuccessGet(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests", &tokenRequestsCollection) + tests.DoSuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests", &tokenRequestsCollection) - assert.Len(t, tokenRequestsCollection, 2) + assert.Len(s.T(), tokenRequestsCollection, 2) } -func Test_Close2FaTokenRequest(t *testing.T) { +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestClose2FaTokenRequest() { var tokenRequest *tests.AuthTokenRequestResponse - browserExtension := tests.CreateBrowserExtension(t, "go-ext") + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") tokenRequestPayload := []byte(`{"domain":"facebook.com"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", tokenRequestPayload, &tokenRequest) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", tokenRequestPayload, &tokenRequest) closeTokenRequestPayload := []byte(`{"status":"completed"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) var closedTokenRequest *tests.AuthTokenRequestResponse - tests.DoSuccessGet(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &closedTokenRequest) - assert.Equal(t, "completed", closedTokenRequest.Status) + tests.DoSuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &closedTokenRequest) + assert.Equal(s.T(), "completed", closedTokenRequest.Status) } -func Test_CloseNotExisting2FaTokenRequest(t *testing.T) { +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestCloseNotExisting2FaTokenRequest() { notExistingTokenRequestId := uuid.New() - browserExtension := tests.CreateBrowserExtension(t, "go-ext") + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") closeTokenRequestPayload := []byte(`{"status":"completed"}`) response := tests.DoPost("browser_extensions/"+browserExtension.Id+"/2fa_requests/"+notExistingTokenRequestId.String()+"/commands/close_2fa_request", closeTokenRequestPayload, nil) - assert.Equal(t, 404, response.StatusCode) + assert.Equal(s.T(), 404, response.StatusCode) } -func Test_DoNotReturnClosed2FaRequests(t *testing.T) { +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestDoNotReturnClosed2FaRequests() { var tokenRequest *tests.AuthTokenRequestResponse - browserExtension := tests.CreateBrowserExtension(t, "go-ext") + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") tokenRequestPayload := []byte(`{"domain":"facebook.com"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", tokenRequestPayload, &tokenRequest) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", tokenRequestPayload, &tokenRequest) closeTokenRequestPayload := []byte(`{"status":"completed"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) var response []*tests.AuthTokenRequestResponse - tests.DoSuccessGet(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests", &response) - assert.Len(t, response, 0) + tests.DoSuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests", &response) + assert.Len(s.T(), response, 0) } -func Test_Terminate2FaRequest(t *testing.T) { +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestTerminate2FaRequest() { var tokenRequest *tests.AuthTokenRequestResponse - browserExtension := tests.CreateBrowserExtension(t, "go-ext") + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") tokenRequestPayload := []byte(`{"domain":"facebook.com"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", tokenRequestPayload, &tokenRequest) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", tokenRequestPayload, &tokenRequest) closeTokenRequestPayload := []byte(`{"status":"terminated"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) var response *tests.AuthTokenRequestResponse - tests.DoSuccessGet(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &response) - assert.Equal(t, "terminated", response.Status) + tests.DoSuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &response) + assert.Equal(s.T(), "terminated", response.Status) } -func Test_Close2FaRequest(t *testing.T) { - device, devicePubKey := tests.CreateDevice(t, "SM-955F", "fcm-token") - browserExtension := tests.CreateBrowserExtension(t, "go-ext") - tests.PairDeviceWithBrowserExtension(t, devicePubKey, browserExtension, device) +func (s *BrowserExtensionTwoFactorAuthTestSuite) TestClose2FaRequest() { + device, devicePubKey := tests.CreateDevice(s.T(), "SM-955F", "fcm-token") + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") + tests.PairDeviceWithBrowserExtension(s.T(), devicePubKey, browserExtension, device) var tokenRequest *tests.AuthTokenRequestResponse request2FaTokenPayload := []byte(`{"domain":"domain.com"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", request2FaTokenPayload, &tokenRequest) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/commands/request_2fa_token", request2FaTokenPayload, &tokenRequest) closeTokenRequestPayload := []byte(`{"status":"completed"}`) - tests.DoSuccessPost(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) + tests.DoSuccessPost(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id+"/commands/close_2fa_request", closeTokenRequestPayload, nil) var closedTokenRequest *tests.AuthTokenRequestResponse - tests.DoSuccessGet(t, "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &closedTokenRequest) - assert.Equal(t, "completed", closedTokenRequest.Status) + tests.DoSuccessGet(s.T(), "browser_extensions/"+browserExtension.Id+"/2fa_requests/"+tokenRequest.Id, &closedTokenRequest) + assert.Equal(s.T(), "completed", closedTokenRequest.Status) } diff --git a/tests/browser_extension/browser_extension_2fa_test.go b/tests/browser_extension/browser_extension_2fa_test.go index 4393126..446b6e9 100644 --- a/tests/browser_extension/browser_extension_2fa_test.go +++ b/tests/browser_extension/browser_extension_2fa_test.go @@ -3,35 +3,50 @@ package tests import ( "encoding/json" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" "github.com/twofas/2fas-server/tests" "testing" ) -func Test_BrowserExtensionAuthFullFlow(t *testing.T) { - device, devicePubKey := tests.CreateDevice(t, "SM-955F", "some-token") - browserExtension := tests.CreateBrowserExtension(t, "go-ext") +func TestTwoFactorAuthTestSuite(t *testing.T) { + suite.Run(t, new(TwoFactorAuthTestSuite)) +} + +type TwoFactorAuthTestSuite struct { + suite.Suite +} + +func (s *TwoFactorAuthTestSuite) SetupTest() { + tests.DoSuccessDelete(s.T(), "/mobile/devices") + tests.DoSuccessDelete(s.T(), "/browser_extensions") + tests.DoSuccessDelete(s.T(), "browser_extensions/devices") +} + +func (s *TwoFactorAuthTestSuite) TestBrowserExtensionAuthFullFlow() { + device, devicePubKey := tests.CreateDevice(s.T(), "SM-955F", "some-token") + browserExtension := tests.CreateBrowserExtension(s.T(), "go-ext") websocketTestListener := tests.NewWebsocketTestListener("browser_extensions/" + browserExtension.Id) websocketConnection := websocketTestListener.StartListening() defer websocketConnection.Close() - tests.PairDeviceWithBrowserExtension(t, devicePubKey, browserExtension, device) + tests.PairDeviceWithBrowserExtension(s.T(), devicePubKey, browserExtension, device) - assertDeviceHasPairedExtension(t, device, browserExtension) - assertBrowserExtensionHasPairedDevice(t, browserExtension, device) + assertDeviceHasPairedExtension(s.T(), device, browserExtension) + assertBrowserExtensionHasPairedDevice(s.T(), browserExtension, device) expectedPairingSuccessWebsocket := createPairingSuccessWebsocketMessage(browserExtension, device, devicePubKey) - websocketTestListener.AssertMessageHasBeenReceived(t, expectedPairingSuccessWebsocket) + websocketTestListener.AssertMessageHasBeenReceived(s.T(), expectedPairingSuccessWebsocket) - tokenRequest := tests.Request2FaToken(t, "facebook.com", browserExtension.Id) + tokenRequest := tests.Request2FaToken(s.T(), "facebook.com", browserExtension.Id) extensionTokenRequestWebsocketListener := tests.NewWebsocketTestListener("browser_extensions/" + browserExtension.Id + "/2fa_requests/" + tokenRequest.Id) extensionTokenRequestWebsocketConnection := extensionTokenRequestWebsocketListener.StartListening() defer extensionTokenRequestWebsocketConnection.Close() - tests.Send2FaTokenToExtension(t, browserExtension.Id, device.Id, tokenRequest.Id, "2fa-token") + tests.Send2FaTokenToExtension(s.T(), browserExtension.Id, device.Id, tokenRequest.Id, "2fa-token") expected2FaTokenWebsocket := createBrowserExtensionReceived2FaTokenMessage(browserExtension.Id, device.Id, tokenRequest.Id) - extensionTokenRequestWebsocketListener.AssertMessageHasBeenReceived(t, expected2FaTokenWebsocket) + extensionTokenRequestWebsocketListener.AssertMessageHasBeenReceived(s.T(), expected2FaTokenWebsocket) } func createBrowserExtensionReceived2FaTokenMessage(extensionId, deviceId, requestId string) string {