@@ -0,0 +1,49 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAdminInvitationAndInvitedUserActivationRequests(t *testing.T) {
|
||||
var inviteSeen, activationSeen bool
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/v1/admin/users":
|
||||
var input AdminUserInviteInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inviteSeen = input.Name == "Ada" && input.Email == "ada@example.com"
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
_, _ = w.Write([]byte(`{"user":{"id":42,"name":"Ada","email":"ada@example.com"}}`))
|
||||
case r.Method == http.MethodPut && r.URL.Path == "/v1/users/activated":
|
||||
var input map[string]string
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
activationSeen = input["token"] == "activation-token" && input["password"] == "new-password"
|
||||
_, _ = w.Write([]byte(`{"user":{"id":42,"activated":true}}`))
|
||||
case r.Method == http.MethodDelete && r.URL.Path == "/v1/admin/users/42":
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
apiClient := newTestClient(t, handler)
|
||||
|
||||
if _, _, err := apiClient.InviteAdminUser(t.Context(), AdminUserInviteInput{Name: "Ada", Email: "ada@example.com"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := apiClient.ActivateInvitedUser(t.Context(), "activation-token", "new-password"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := apiClient.DeleteAdminUser(t.Context(), 42); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !inviteSeen || !activationSeen {
|
||||
t.Fatalf("request payloads not received: invite=%t activation=%t", inviteSeen, activationSeen)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user