MCPcopy
hub / github.com/daptin/daptin / signUpAndGetToken

Function signUpAndGetToken

websocket_test.go:127–174  ·  view source on GitHub ↗

signUpAndGetToken returns a JWT token, creating a user if needed. Works both standalone (guest signup open) and after TestServerApis (guest signup locked).

(t testing.TB)

Source from the content-addressed store, hash-verified

125// signUpAndGetToken returns a JWT token, creating a user if needed.
126// Works both standalone (guest signup open) and after TestServerApis (guest signup locked).
127func signUpAndGetToken(t testing.TB) string {
128 if t != nil {
129 t.Helper()
130 }
131 wsTokenOnce.Do(func() {
132 client := req.New()
133 client.SetTimeout(30 * time.Second)
134
135 // Try signing in as test@gmail.com (created by TestServerApis)
136 resp, err := client.Post(wsBaseAddress+"/action/user_account/signin", req.BodyJSON(map[string]interface{}{
137 "attributes": map[string]interface{}{
138 "email": "test@gmail.com",
139 "password": "tester123",
140 },
141 }))
142 if err == nil {
143 if tok := extractToken(resp); tok != "" {
144 wsToken = tok
145 return
146 }
147 }
148
149 // TestServerApis hasn't run — sign up as guest
150 client.Post(wsBaseAddress+"/action/user_account/signup", req.BodyJSON(map[string]interface{}{
151 "attributes": map[string]interface{}{
152 "email": "test@gmail.com",
153 "name": "test",
154 "password": "tester123",
155 "passwordConfirm": "tester123",
156 },
157 }))
158
159 resp, err = client.Post(wsBaseAddress+"/action/user_account/signin", req.BodyJSON(map[string]interface{}{
160 "attributes": map[string]interface{}{
161 "email": "test@gmail.com",
162 "password": "tester123",
163 },
164 }))
165 if err != nil {
166 panic(fmt.Sprintf("signin failed: %v", err))
167 }
168 wsToken = extractToken(resp)
169 if wsToken == "" {
170 panic("no token after signup+signin")
171 }
172 })
173 return wsToken
174}
175
176func extractToken(resp *req.Resp) string {
177 var signInResp []interface{}

Calls 1

extractTokenFunction · 0.85

Tested by

no test coverage detected