2022-12-31 10:22:38 +01:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2023-12-14 10:07:59 +01:00
|
|
|
"encoding/json"
|
2023-10-24 09:27:34 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
"github.com/jaswdr/faker"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2024-03-16 15:41:30 +01:00
|
|
|
"github.com/twofas/2fas-server/e2e-tests"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/app/queries"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIconsRequestsTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(IconsRequestsTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type IconsRequestsTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) SetupTest() {
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.RemoveAllMobileWebServices(s.T())
|
|
|
|
e2e_tests.RemoveAllMobileIcons(s.T())
|
|
|
|
e2e_tests.RemoveAllMobileIconsCollections(s.T())
|
|
|
|
e2e_tests.RemoveAllMobileIconsRequests(s.T())
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestCreateIconRequest() {
|
|
|
|
iconRequest := createIconRequest(s.T(), "service")
|
|
|
|
|
|
|
|
assert.Equal(s.T(), "service", iconRequest.ServiceName)
|
2023-02-03 17:00:45 +01:00
|
|
|
assert.Equal(s.T(), "desc", iconRequest.Description)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-02-07 11:38:28 +01:00
|
|
|
func (s *IconsRequestsTestSuite) TestCreateIconRequestWithNotAllowedIconDimensions() {
|
|
|
|
img := faker.New().Image().Image(120, 60)
|
|
|
|
|
|
|
|
pngImg, err := ioutil.ReadFile(img.Name())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.T().Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
iconBase64Encoded := base64.StdEncoding.EncodeToString(pngImg)
|
|
|
|
|
|
|
|
payload := []byte(`
|
|
|
|
{
|
|
|
|
"caller_id":"some-caller-uniq-name",
|
|
|
|
"service_name":"some-service",
|
|
|
|
"issuers": ["fb"],
|
|
|
|
"description":"desc",
|
|
|
|
"light_icon":"` + iconBase64Encoded + `"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
var iconRequest *queries.IconRequestPresenter
|
|
|
|
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAPIPostAndAssertCode(s.T(), 400, "mobile/icons/requests", payload, &iconRequest)
|
2023-02-07 11:38:28 +01:00
|
|
|
}
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
func (s *IconsRequestsTestSuite) TestDeleteIconRequest() {
|
|
|
|
iconRequest := createIconRequest(s.T(), "service")
|
|
|
|
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/icons/requests/"+iconRequest.Id)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2024-03-16 15:41:30 +01:00
|
|
|
response := e2e_tests.DoAPIGet(s.T(), "mobile/icons/requests/"+iconRequest.Id, nil)
|
2022-12-31 10:22:38 +01:00
|
|
|
assert.Equal(s.T(), 404, response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestFindAllIconsRequests() {
|
|
|
|
createIconRequest(s.T(), "service1")
|
|
|
|
createIconRequest(s.T(), "service2")
|
|
|
|
|
|
|
|
var iconsRequests []*queries.IconRequestPresenter
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons/requests", &iconsRequests)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Len(s.T(), iconsRequests, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestFindIconRequest() {
|
|
|
|
iconRequest := createIconRequest(s.T(), "service")
|
|
|
|
|
|
|
|
var searchResult *queries.IconPresenter
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAdminSuccessGet(s.T(), "mobile/icons/requests/"+iconRequest.Id, &searchResult)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "service", searchResult.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestTransformIconRequestIntoWebService() {
|
|
|
|
iconRequest := createIconRequest(s.T(), "service")
|
|
|
|
|
|
|
|
var result *queries.WebServicePresenter
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/icons/requests/"+iconRequest.Id+"/commands/transform_to_web_service", nil, &result)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), "service", result.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestTransformSingleIconRequestsIntoWebServiceFromManyRequestsWithSameServiceName() {
|
|
|
|
iconRequest := createIconRequest(s.T(), "service")
|
|
|
|
createIconRequest(s.T(), "service")
|
|
|
|
|
|
|
|
var result *queries.WebServicePresenter
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/icons/requests/"+iconRequest.Id+"/commands/transform_to_web_service", nil, &result)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
var icons []*queries.IconPresenter
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAPIGet(s.T(), "mobile/icons", &icons)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Len(s.T(), icons, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestTransformIconRequestWithAlreadyExistingWebService() {
|
|
|
|
webService := createWebService(s.T())
|
|
|
|
iconRequest := createIconRequest(s.T(), webService.Name)
|
|
|
|
|
|
|
|
var result *queries.WebServicePresenter
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAdminPostAndAssertCode(s.T(), 409, "mobile/icons/requests/"+iconRequest.Id+"/commands/transform_to_web_service", nil, &result)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IconsRequestsTestSuite) TestUpdateWebServiceFromIconRequest() {
|
|
|
|
iconRequest := createIconRequest(s.T(), "service")
|
|
|
|
webService := createWebService(s.T())
|
|
|
|
|
|
|
|
var result *queries.WebServicePresenter
|
|
|
|
payload := []byte(`{"web_service_id":"` + webService.Id + `"}`)
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAdminAPISuccessPost(s.T(), "mobile/icons/requests/"+iconRequest.Id+"/commands/update_web_service", payload, &result)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), webService.Name, result.Name)
|
2023-12-14 10:07:59 +01:00
|
|
|
|
|
|
|
var iconsCollections []string
|
|
|
|
if err := json.Unmarshal(result.IconsCollections, &iconsCollections); err != nil {
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
assert.Equal(s.T(), webService.IconsCollections, iconsCollections, "icons collections id should not change")
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createIconRequest(t *testing.T, serviceName string) *queries.IconRequestPresenter {
|
|
|
|
if serviceName == "" {
|
|
|
|
serviceName = "some-service"
|
|
|
|
}
|
|
|
|
|
|
|
|
img := faker.New().Image().Image(120, 120)
|
|
|
|
|
|
|
|
pngImg, err := ioutil.ReadFile(img.Name())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
iconBase64Encoded := base64.StdEncoding.EncodeToString(pngImg)
|
|
|
|
|
|
|
|
payload := []byte(`
|
|
|
|
{
|
|
|
|
"caller_id":"some-caller-uniq-name",
|
|
|
|
"service_name":"` + serviceName + `",
|
|
|
|
"issuers": ["fb"],
|
|
|
|
"description":"desc",
|
|
|
|
"light_icon":"` + iconBase64Encoded + `"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
var iconRequest *queries.IconRequestPresenter
|
|
|
|
|
2024-03-16 15:41:30 +01:00
|
|
|
e2e_tests.DoAPISuccessPost(t, "mobile/icons/requests", payload, &iconRequest)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
return iconRequest
|
|
|
|
}
|