2fas-server/tests/mobile/mobile_device_test.go

54 lines
1.6 KiB
Go
Raw Normal View History

2023-03-12 14:59:53 +01:00
package tests
import (
"fmt"
"net/http"
"testing"
2023-03-12 14:59:53 +01:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/twofas/2fas-server/tests"
)
func TestMobileDeviceTestSuite(t *testing.T) {
suite.Run(t, new(MobileDeviceTestSuite))
}
type MobileDeviceTestSuite struct {
suite.Suite
}
func (s *MobileDeviceTestSuite) SetupTest() {
tests.RemoveAllMobileDevices(s.T())
2023-03-12 14:59:53 +01:00
}
func (s *MobileDeviceTestSuite) TestCreateMobileDevice() {
type testCase struct {
deviceName string
2023-10-24 09:59:36 +02:00
fcmToken string
2023-03-12 14:59:53 +01:00
expectedHttpCode int
}
2023-10-24 09:59:36 +02:00
defaultFCMToken := "some-fake-token"
2023-03-12 14:59:53 +01:00
testsCases := []testCase{
2023-10-24 09:59:36 +02:00
{deviceName: "", fcmToken: defaultFCMToken, expectedHttpCode: 400},
{deviceName: " ", fcmToken: defaultFCMToken, expectedHttpCode: 400},
{deviceName: " ", fcmToken: defaultFCMToken, expectedHttpCode: 400},
{deviceName: "john`s android", fcmToken: defaultFCMToken, expectedHttpCode: 200},
{deviceName: "john ", fcmToken: defaultFCMToken, expectedHttpCode: 200},
{deviceName: " john doe", fcmToken: defaultFCMToken, expectedHttpCode: 200},
// empty FCM token should be also valid.
{deviceName: " john doe", fcmToken: "", expectedHttpCode: 200},
2023-03-12 14:59:53 +01:00
}
for _, tc := range testsCases {
2023-10-24 09:59:36 +02:00
response := createDevice(s.T(), tc.deviceName, tc.fcmToken)
2023-03-12 14:59:53 +01:00
assert.Equal(s.T(), tc.expectedHttpCode, response.StatusCode)
}
}
2023-10-24 09:59:36 +02:00
func createDevice(t *testing.T, name, fcmToken string) *http.Response {
2023-03-12 14:59:53 +01:00
payload := []byte(fmt.Sprintf(`{"name":"%s","platform":"android","fcm_token":"%s"}`, name, fcmToken))
return tests.DoAPIRequest(t, "mobile/devices", http.MethodPost, payload, nil)
2023-03-12 14:59:53 +01:00
}