2023-03-12 14:59:53 +01:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-24 09:27:34 +02:00
|
|
|
"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() {
|
2023-10-24 09:27:34 +02:00
|
|
|
tests.RemoveAllMobileDevices(s.T())
|
2023-03-12 14:59:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MobileDeviceTestSuite) TestCreateMobileDevice() {
|
|
|
|
type testCase struct {
|
|
|
|
deviceName string
|
|
|
|
expectedHttpCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
testsCases := []testCase{
|
|
|
|
{deviceName: "", expectedHttpCode: 400},
|
|
|
|
{deviceName: " ", expectedHttpCode: 400},
|
|
|
|
{deviceName: " ", expectedHttpCode: 400},
|
|
|
|
{deviceName: "john`s android", expectedHttpCode: 200},
|
|
|
|
{deviceName: "john ", expectedHttpCode: 200},
|
|
|
|
{deviceName: " john doe", expectedHttpCode: 200},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testsCases {
|
2023-10-24 09:27:34 +02:00
|
|
|
response := createDevice(s.T(), tc.deviceName)
|
2023-03-12 14:59:53 +01:00
|
|
|
|
|
|
|
assert.Equal(s.T(), tc.expectedHttpCode, response.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 09:27:34 +02:00
|
|
|
func createDevice(t *testing.T, name string) *http.Response {
|
2023-03-12 14:59:53 +01:00
|
|
|
fcmToken := "some-fake-token"
|
|
|
|
payload := []byte(fmt.Sprintf(`{"name":"%s","platform":"android","fcm_token":"%s"}`, name, fcmToken))
|
|
|
|
|
2023-10-24 09:27:34 +02:00
|
|
|
return tests.DoAPIRequest(t, "mobile/devices", http.MethodPost, payload, nil)
|
2023-03-12 14:59:53 +01:00
|
|
|
}
|