2022-12-31 10:22:38 +01:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2023-03-12 14:58:54 +01:00
|
|
|
"fmt"
|
2023-10-24 09:27:34 +02:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2023-03-12 14:58:54 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/common/crypto"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/tests"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBrowserExtensionTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(BrowserExtensionTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type BrowserExtensionTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BrowserExtensionTestSuite) SetupTest() {
|
2023-10-24 09:27:34 +02:00
|
|
|
tests.RemoveAllBrowserExtensions(s.T())
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-03-12 14:58:54 +01:00
|
|
|
func (s *BrowserExtensionTestSuite) TestCreateBrowserExtension() {
|
|
|
|
type testCase struct {
|
|
|
|
extensionName string
|
|
|
|
expectedHttpCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
testsCases := []testCase{
|
|
|
|
{extensionName: "", expectedHttpCode: 400},
|
|
|
|
{extensionName: " ", expectedHttpCode: 400},
|
|
|
|
{extensionName: " ", expectedHttpCode: 400},
|
|
|
|
{extensionName: "abc", expectedHttpCode: 200},
|
|
|
|
{extensionName: "efg ", expectedHttpCode: 200},
|
|
|
|
{extensionName: " ab123 ", expectedHttpCode: 200},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testsCases {
|
2023-10-24 09:27:34 +02:00
|
|
|
response := createBrowserExtension(s.T(), tc.extensionName)
|
2023-03-12 14:58:54 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), tc.expectedHttpCode, response.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
func (s *BrowserExtensionTestSuite) TestUpdateBrowserExtension() {
|
|
|
|
browserExt := tests.CreateBrowserExtension(s.T(), "go-test")
|
|
|
|
|
|
|
|
payload := []byte(`{"name": "updated-extension-name"}`)
|
2023-10-24 09:27:34 +02:00
|
|
|
tests.DoAPISuccessPut(s.T(), "/browser_extensions/"+browserExt.Id, payload, nil)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
var browserExtension *tests.BrowserExtensionResponse
|
2023-10-24 09:27:34 +02:00
|
|
|
tests.DoAPISuccessGet(s.T(), "/browser_extensions/"+browserExt.Id, &browserExtension)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "updated-extension-name", browserExtension.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BrowserExtensionTestSuite) TestUpdateNotExistingBrowserExtension() {
|
|
|
|
id := uuid.New()
|
|
|
|
|
|
|
|
payload := []byte(`{"name": "updated-extension-name"}`)
|
2023-10-24 09:27:34 +02:00
|
|
|
response := tests.DoAPIRequest(s.T(), "/browser_extensions/"+id.String(), http.MethodPut, payload, nil)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 404, response.StatusCode)
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:30:42 +01:00
|
|
|
func (s *BrowserExtensionTestSuite) TestUpdateBrowserExtensionSetEmptyName() {
|
|
|
|
browserExt := tests.CreateBrowserExtension(s.T(), "go-test")
|
|
|
|
|
|
|
|
payload := []byte(`{"name": ""}`)
|
2023-10-24 09:27:34 +02:00
|
|
|
response := tests.DoAPIRequest(s.T(), "/browser_extensions/"+browserExt.Id, http.MethodPut, payload, nil)
|
2023-03-18 15:30:42 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 400, response.StatusCode)
|
|
|
|
}
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
func (s *BrowserExtensionTestSuite) TestDoNotFindNotExistingExtension() {
|
|
|
|
notExistingId := uuid.New()
|
|
|
|
|
|
|
|
var browserExtension *tests.BrowserExtensionResponse
|
2023-10-24 09:27:34 +02:00
|
|
|
response := tests.DoAPIGet(s.T(), "/browser_extensions/"+notExistingId.String(), &browserExtension)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 404, response.StatusCode)
|
|
|
|
}
|
2023-03-12 14:58:54 +01:00
|
|
|
|
2023-10-24 09:27:34 +02:00
|
|
|
func createBrowserExtension(t *testing.T, name string) *http.Response {
|
2023-03-12 14:58:54 +01:00
|
|
|
keyPair := crypto.GenerateKeyPair(2048)
|
|
|
|
|
|
|
|
pubKey := crypto.PublicKeyToBase64(keyPair.PublicKey)
|
|
|
|
|
|
|
|
payload := []byte(fmt.Sprintf(`{"name":"%s","browser_name":"go-browser","browser_version":"0.1","public_key":"%s"}`, name, pubKey))
|
|
|
|
|
2023-10-24 09:27:34 +02:00
|
|
|
return tests.DoAPIRequest(t, "/browser_extensions", http.MethodPost, payload, nil)
|
2023-03-12 14:58:54 +01:00
|
|
|
|
|
|
|
}
|