-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathapi_test.go
More file actions
134 lines (114 loc) · 3.1 KB
/
api_test.go
File metadata and controls
134 lines (114 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package testing
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)
type request struct {
Method string
URL string
Body []byte
}
func TestAPI(t *testing.T) { //nolint:cyclop
t.Parallel()
requests := [][]request{
{
{"GET", "/ui/login", nil},
{"GET", "/api/config", nil},
},
{
{"POST", "/auth/local/login", []byte(`{"email":"admin@catalyst-soar.com","password":"password123"}`)},
},
{
{"GET", "/auth/user", nil},
{"GET", "/api/sidebar", nil},
{"GET", "/ui/groups", nil},
{"GET", "/api/tickets", nil},
{"GET", "/api/tickets", nil},
{"GET", "/api/tasks", nil},
{"GET", "/auth/user", nil},
{"GET", "/auth/user", nil},
{"GET", "/api/sidebar", nil},
{"GET", "/api/groups", nil},
{"GET", "/api/config", nil},
},
{
{"POST", "/api/groups", []byte(`{"name":"playwright-59537c9d-772f-45f0-a8f0-bb99656fa7ed","permissions":["ticket:read"]}`)},
},
{
{"GET", "/api/groups/ID", nil},
{"GET", "/api/groups", nil},
{"GET", "/api/config", nil},
},
{
{"GET", "/api/groups/ID/parents", nil},
{"GET", "/api/groups/ID/permissions", nil},
{"GET", "/api/groups/ID/children", nil},
{"GET", "/api/groups/ID/users", nil},
{"GET", "/api/users", nil},
},
{
{"GET", "/ui/login", nil},
{"GET", "/api/config", nil},
},
{
{"POST", "/auth/local/login", []byte(`{"email":"admin@catalyst-soar.com","password":"password123"}`)},
},
{
{"GET", "/auth/user", nil},
{"GET", "/api/sidebar", nil},
{"GET", "/ui/groups", nil},
{"GET", "/api/tickets", nil},
{"GET", "/api/tickets", nil},
{"GET", "/api/tasks", nil},
{"GET", "/auth/user", nil},
{"GET", "/auth/user", nil},
{"GET", "/api/sidebar", nil},
{"GET", "/api/groups", nil},
{"GET", "/api/config", nil},
},
{
{"POST", "/api/groups", []byte(`{"name":"playwright-c9bdcbf8-6aba-4974-9c12-f77fa7f15b34","permissions":["ticket:read"]}`)},
},
}
app, cleanup, _ := App(t)
t.Cleanup(cleanup)
id, token := "", ""
for _, batch := range requests {
var wg sync.WaitGroup
wg.Add(len(batch))
for _, req := range batch {
go func() {
defer wg.Done()
url := req.URL
if strings.Contains(url, "ID") {
url = strings.ReplaceAll(url, "ID", id)
}
ctx, cancel := context.WithTimeout(t.Context(), time.Second*10)
defer cancel()
r, err := http.NewRequestWithContext(ctx, req.Method, url, bytes.NewReader(req.Body))
assert.NoError(t, err)
if token != "" {
r.Header.Set("Authorization", "Bearer "+token)
}
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
assert.Equal(t, 200, w.Code, "expected status code 200: %s for %s %s", w.Body.String(), req.Method, url)
if w.Code == 200 && req.Method == http.MethodPost && gjson.Get(w.Body.String(), "id").Exists() {
id = gjson.Get(w.Body.String(), "id").String()
}
if w.Code == 200 && req.Method == http.MethodPost && gjson.Get(w.Body.String(), "token").Exists() {
token = gjson.Get(w.Body.String(), "token").String()
}
}()
}
wg.Wait()
}
}