Skip to content

Commit fe0dadb

Browse files
committed
[e2e] add priority validation tests
1 parent fa25bea commit fe0dadb

File tree

6 files changed

+121
-46
lines changed

6 files changed

+121
-46
lines changed

test/e2e/go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ go 1.23.0
44

55
toolchain go1.23.2
66

7-
require github.com/go-resty/resty/v2 v2.16.2
7+
require (
8+
github.com/capcom6/go-helpers v0.2.0
9+
github.com/go-resty/resty/v2 v2.16.2
10+
)
811

912
require golang.org/x/net v0.37.0 // indirect

test/e2e/go.sum

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

test/e2e/main_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import (
1111
"github.com/go-resty/resty/v2"
1212
)
1313

14-
const (
15-
PublicURL = "http://localhost:3000/api"
16-
PrivateURL = "http://localhost:3001/api"
17-
)
18-
1914
func isOnline() bool {
2015
for _, v := range []string{PublicURL, PrivateURL} {
2116
_, err := resty.New().
@@ -34,6 +29,8 @@ func isOnline() bool {
3429
}
3530

3631
func TestMain(m *testing.M) {
32+
hasErrors := false
33+
3734
log.Println("running e2e tests")
3835

3936
if _, ok := os.LookupEnv("CI"); !ok {
@@ -46,13 +43,18 @@ func TestMain(m *testing.M) {
4643
log.Fatal(fmt.Errorf("docker-compose down -v: %w", err))
4744
}
4845
log.Println("e2e tests finished")
46+
47+
if hasErrors {
48+
log.Fatal("e2e tests failed")
49+
}
4950
}()
5051
}
5152

5253
startedAt := time.Now()
5354
for {
54-
if time.Since(startedAt) > 20*time.Second {
55+
if time.Since(startedAt) > 30*time.Second {
5556
log.Println("timeout")
57+
hasErrors = true
5658
return
5759
}
5860

test/e2e/mobile_test.go

+6-39
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ import (
44
"encoding/base64"
55
"encoding/json"
66
"testing"
7-
"time"
8-
9-
"github.com/go-resty/resty/v2"
10-
)
11-
12-
var (
13-
publicClient = resty.New().
14-
SetBaseURL(PublicURL + "/mobile/v1").
15-
SetTimeout(300 * time.Millisecond)
16-
privateClient = resty.New().
17-
SetBaseURL(PrivateURL + "/mobile/v1").
18-
SetTimeout(300 * time.Millisecond)
197
)
208

219
type mobileRegisterResponse struct {
@@ -24,27 +12,6 @@ type mobileRegisterResponse struct {
2412
Password string `json:"password"`
2513
}
2614

27-
func mobileDeviceRegister(t *testing.T, client *resty.Client) mobileRegisterResponse {
28-
res, err := client.R().
29-
SetHeader("Content-Type", "application/json").
30-
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
31-
Post("device")
32-
if err != nil {
33-
t.Fatal(err)
34-
}
35-
36-
if !res.IsSuccess() {
37-
t.Fatal(res.StatusCode(), res.String())
38-
}
39-
40-
var resp mobileRegisterResponse
41-
if err := json.Unmarshal(res.Body(), &resp); err != nil {
42-
t.Fatal(err)
43-
}
44-
45-
return resp
46-
}
47-
4815
func TestPublicDeviceRegister(t *testing.T) {
4916
cases := []struct {
5017
name string
@@ -74,7 +41,7 @@ func TestPublicDeviceRegister(t *testing.T) {
7441

7542
for _, c := range cases {
7643
t.Run(c.name, func(t *testing.T) {
77-
res, err := publicClient.R().
44+
res, err := publicMobileClient.R().
7845
SetHeader("Content-Type", "application/json").
7946
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
8047
SetHeaders(c.headers).
@@ -117,7 +84,7 @@ func TestPrivateDeviceRegister(t *testing.T) {
11784
},
11885
}
11986

120-
client := privateClient
87+
client := privateMobileClient
12188

12289
for _, c := range cases {
12390
t.Run(c.name, func(t *testing.T) {
@@ -138,7 +105,7 @@ func TestPrivateDeviceRegister(t *testing.T) {
138105
}
139106

140107
func TestPublicDevicePasswordChange(t *testing.T) {
141-
device := mobileDeviceRegister(t, publicClient)
108+
device := mobileDeviceRegister(t, publicMobileClient)
142109

143110
cases := []struct {
144111
name string
@@ -190,7 +157,7 @@ func TestPublicDevicePasswordChange(t *testing.T) {
190157

191158
for _, c := range cases {
192159
t.Run(c.name, func(t *testing.T) {
193-
res, err := publicClient.R().
160+
res, err := publicMobileClient.R().
194161
SetHeader("Content-Type", "application/json").
195162
SetBody(c.body).
196163
SetHeaders(c.headers).
@@ -210,7 +177,7 @@ func TestPublicDeviceRegisterWithCredentials(t *testing.T) {
210177
// won't work with registration rate limits
211178
t.SkipNow()
212179

213-
firstDevice := mobileDeviceRegister(t, publicClient)
180+
firstDevice := mobileDeviceRegister(t, publicMobileClient)
214181

215182
cases := []struct {
216183
name string
@@ -239,7 +206,7 @@ func TestPublicDeviceRegisterWithCredentials(t *testing.T) {
239206

240207
for _, c := range cases {
241208
t.Run(c.name, func(t *testing.T) {
242-
res, err := publicClient.R().
209+
res, err := publicMobileClient.R().
243210
SetHeader("Content-Type", "application/json").
244211
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
245212
SetHeaders(c.headers).

test/e2e/priority_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package e2e
2+
3+
import (
4+
"testing"
5+
6+
"github.com/capcom6/go-helpers/anys"
7+
)
8+
9+
func TestPriorityPost(t *testing.T) {
10+
cases := []struct {
11+
name string
12+
priority *int
13+
expectedStatusCode int
14+
}{
15+
{
16+
name: "min priority",
17+
priority: anys.AsPointer(-128),
18+
expectedStatusCode: 202,
19+
},
20+
{
21+
name: "max priority",
22+
priority: anys.AsPointer(127),
23+
expectedStatusCode: 202,
24+
},
25+
{
26+
name: "invalid priority",
27+
priority: anys.AsPointer(128),
28+
expectedStatusCode: 400,
29+
},
30+
{
31+
name: "invalid priority",
32+
priority: anys.AsPointer(-129),
33+
expectedStatusCode: 400,
34+
},
35+
{
36+
name: "default priority",
37+
priority: nil,
38+
expectedStatusCode: 202,
39+
},
40+
}
41+
42+
req := map[string]any{
43+
"message": "test",
44+
"phoneNumbers": []string{
45+
"+79999999999",
46+
},
47+
}
48+
49+
credentials := mobileDeviceRegister(t, publicMobileClient)
50+
client := publicUserClient.SetBasicAuth(credentials.Login, credentials.Password)
51+
52+
for _, c := range cases {
53+
t.Run(c.name, func(t *testing.T) {
54+
if c.priority != nil {
55+
req["priority"] = *c.priority
56+
} else {
57+
delete(req, "priority")
58+
}
59+
res, err := client.R().
60+
SetHeader("Content-Type", "application/json").
61+
SetBody(req).
62+
Post("messages")
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
67+
if res.StatusCode() != c.expectedStatusCode {
68+
t.Fatal(res.StatusCode(), res.String())
69+
}
70+
})
71+
}
72+
}

test/e2e/utils_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package e2e
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/go-resty/resty/v2"
8+
)
9+
10+
func mobileDeviceRegister(t *testing.T, client *resty.Client) mobileRegisterResponse {
11+
res, err := client.R().
12+
SetHeader("Content-Type", "application/json").
13+
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
14+
Post("device")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
if !res.IsSuccess() {
20+
t.Fatal(res.StatusCode(), res.String())
21+
}
22+
23+
var resp mobileRegisterResponse
24+
if err := json.Unmarshal(res.Body(), &resp); err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
return resp
29+
}

0 commit comments

Comments
 (0)