2fas-server/e2e-tests/icons/icons_test.go

102 lines
2.1 KiB
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package tests
import (
"encoding/base64"
"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
query "github.com/twofas/2fas-server/internal/api/icons/app/queries"
2022-12-31 10:22:38 +01:00
)
func createIcon(t *testing.T) *query.IconPresenter {
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(`
{
"name":"facebook",
"description":"desc",
"type":"light",
"icon":"` + iconBase64Encoded + `"
}
`)
var icon *query.IconPresenter
2024-03-16 15:41:30 +01:00
e2e_tests.DoAdminAPISuccessPost(t, "mobile/icons", payload, &icon)
2022-12-31 10:22:38 +01:00
return icon
}
func TestIconsTestSuite(t *testing.T) {
suite.Run(t, new(IconsTestSuite))
}
type IconsTestSuite struct {
suite.Suite
}
func (s *IconsTestSuite) SetupTest() {
2024-03-16 15:41:30 +01:00
e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/icons")
2022-12-31 10:22:38 +01:00
}
func (s *IconsTestSuite) TestCreateIcon() {
icon := createIcon(s.T())
assert.Equal(s.T(), "facebook", icon.Name)
}
func (s *IconsTestSuite) TestUpdateIcon() {
icon := createIcon(s.T())
updatePayload := []byte(`
{
"name":"meta"
}
`)
var updatedIcon *query.IconPresenter
2024-03-16 15:41:30 +01:00
e2e_tests.DoAdminSuccessPut(s.T(), "mobile/icons/"+icon.Id, updatePayload, &updatedIcon)
2022-12-31 10:22:38 +01:00
assert.Equal(s.T(), "meta", updatedIcon.Name)
}
func (s *IconsTestSuite) TestDeleteIcon() {
icon := createIcon(s.T())
2024-03-16 15:41:30 +01:00
e2e_tests.DoAdminSuccessDelete(s.T(), "mobile/icons/"+icon.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/"+icon.Id, nil)
2022-12-31 10:22:38 +01:00
assert.Equal(s.T(), 404, response.StatusCode)
}
func (s *IconsTestSuite) TestFindAllIcons() {
createIcon(s.T())
createIcon(s.T())
var Icons []*query.IconPresenter
2024-03-16 15:41:30 +01:00
e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons", &Icons)
2022-12-31 10:22:38 +01:00
assert.Len(s.T(), Icons, 2)
}
func (s *IconsTestSuite) TestFindIcon() {
icon := createIcon(s.T())
var searchResult *query.IconPresenter
2024-03-16 15:41:30 +01:00
e2e_tests.DoAPISuccessGet(s.T(), "mobile/icons/"+icon.Id, &searchResult)
2022-12-31 10:22:38 +01:00
assert.Equal(s.T(), "facebook", searchResult.Name)
}