[e2e] add priority validation tests

This commit is contained in:
Aleksandr Soloshenko 2025-03-30 15:20:39 +07:00 committed by Aleksandr
parent 8cdb4a1d13
commit 747edc8f9c
6 changed files with 121 additions and 46 deletions

View File

@ -4,6 +4,9 @@ go 1.23.0
toolchain go1.23.2
require github.com/go-resty/resty/v2 v2.16.2
require (
github.com/capcom6/go-helpers v0.2.0
github.com/go-resty/resty/v2 v2.16.2
)
require golang.org/x/net v0.37.0 // indirect

View File

@ -1,3 +1,5 @@
github.com/capcom6/go-helpers v0.2.0 h1:OUcUnVbjBiwaTzvyaxkxqRKtrOXv1ifYalQ1NXzFBNM=
github.com/capcom6/go-helpers v0.2.0/go.mod h1:WDqc7HZNqHxUTisArkYIBZtqUfJBVyPWeQI+FMwEzAw=
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg=
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=

View File

@ -11,11 +11,6 @@ import (
"github.com/go-resty/resty/v2"
)
const (
PublicURL = "http://localhost:3000/api"
PrivateURL = "http://localhost:3001/api"
)
func isOnline() bool {
for _, v := range []string{PublicURL, PrivateURL} {
_, err := resty.New().
@ -34,6 +29,8 @@ func isOnline() bool {
}
func TestMain(m *testing.M) {
hasErrors := false
log.Println("running e2e tests")
if _, ok := os.LookupEnv("CI"); !ok {
@ -46,13 +43,18 @@ func TestMain(m *testing.M) {
log.Fatal(fmt.Errorf("docker-compose down -v: %w", err))
}
log.Println("e2e tests finished")
if hasErrors {
log.Fatal("e2e tests failed")
}
}()
}
startedAt := time.Now()
for {
if time.Since(startedAt) > 20*time.Second {
if time.Since(startedAt) > 30*time.Second {
log.Println("timeout")
hasErrors = true
return
}

View File

@ -4,18 +4,6 @@ import (
"encoding/base64"
"encoding/json"
"testing"
"time"
"github.com/go-resty/resty/v2"
)
var (
publicClient = resty.New().
SetBaseURL(PublicURL + "/mobile/v1").
SetTimeout(300 * time.Millisecond)
privateClient = resty.New().
SetBaseURL(PrivateURL + "/mobile/v1").
SetTimeout(300 * time.Millisecond)
)
type mobileRegisterResponse struct {
@ -24,27 +12,6 @@ type mobileRegisterResponse struct {
Password string `json:"password"`
}
func mobileDeviceRegister(t *testing.T, client *resty.Client) mobileRegisterResponse {
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
Post("device")
if err != nil {
t.Fatal(err)
}
if !res.IsSuccess() {
t.Fatal(res.StatusCode(), res.String())
}
var resp mobileRegisterResponse
if err := json.Unmarshal(res.Body(), &resp); err != nil {
t.Fatal(err)
}
return resp
}
func TestPublicDeviceRegister(t *testing.T) {
cases := []struct {
name string
@ -74,7 +41,7 @@ func TestPublicDeviceRegister(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
res, err := publicClient.R().
res, err := publicMobileClient.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
SetHeaders(c.headers).
@ -117,7 +84,7 @@ func TestPrivateDeviceRegister(t *testing.T) {
},
}
client := privateClient
client := privateMobileClient
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
@ -138,7 +105,7 @@ func TestPrivateDeviceRegister(t *testing.T) {
}
func TestPublicDevicePasswordChange(t *testing.T) {
device := mobileDeviceRegister(t, publicClient)
device := mobileDeviceRegister(t, publicMobileClient)
cases := []struct {
name string
@ -190,7 +157,7 @@ func TestPublicDevicePasswordChange(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
res, err := publicClient.R().
res, err := publicMobileClient.R().
SetHeader("Content-Type", "application/json").
SetBody(c.body).
SetHeaders(c.headers).
@ -210,7 +177,7 @@ func TestPublicDeviceRegisterWithCredentials(t *testing.T) {
// won't work with registration rate limits
t.SkipNow()
firstDevice := mobileDeviceRegister(t, publicClient)
firstDevice := mobileDeviceRegister(t, publicMobileClient)
cases := []struct {
name string
@ -239,7 +206,7 @@ func TestPublicDeviceRegisterWithCredentials(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
res, err := publicClient.R().
res, err := publicMobileClient.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
SetHeaders(c.headers).

72
test/e2e/priority_test.go Normal file
View File

@ -0,0 +1,72 @@
package e2e
import (
"testing"
"github.com/capcom6/go-helpers/anys"
)
func TestPriorityPost(t *testing.T) {
cases := []struct {
name string
priority *int
expectedStatusCode int
}{
{
name: "min priority",
priority: anys.AsPointer(-128),
expectedStatusCode: 202,
},
{
name: "max priority",
priority: anys.AsPointer(127),
expectedStatusCode: 202,
},
{
name: "invalid priority",
priority: anys.AsPointer(128),
expectedStatusCode: 400,
},
{
name: "invalid priority",
priority: anys.AsPointer(-129),
expectedStatusCode: 400,
},
{
name: "default priority",
priority: nil,
expectedStatusCode: 202,
},
}
req := map[string]any{
"message": "test",
"phoneNumbers": []string{
"+79999999999",
},
}
credentials := mobileDeviceRegister(t, publicMobileClient)
client := publicUserClient.SetBasicAuth(credentials.Login, credentials.Password)
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.priority != nil {
req["priority"] = *c.priority
} else {
delete(req, "priority")
}
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(req).
Post("messages")
if err != nil {
t.Fatal(err)
}
if res.StatusCode() != c.expectedStatusCode {
t.Fatal(res.StatusCode(), res.String())
}
})
}
}

29
test/e2e/utils_test.go Normal file
View File

@ -0,0 +1,29 @@
package e2e
import (
"encoding/json"
"testing"
"github.com/go-resty/resty/v2"
)
func mobileDeviceRegister(t *testing.T, client *resty.Client) mobileRegisterResponse {
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
Post("device")
if err != nil {
t.Fatal(err)
}
if !res.IsSuccess() {
t.Fatal(res.StatusCode(), res.String())
}
var resp mobileRegisterResponse
if err := json.Unmarshal(res.Body(), &resp); err != nil {
t.Fatal(err)
}
return resp
}