-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinvitations_test.go
More file actions
118 lines (101 loc) · 2.75 KB
/
invitations_test.go
File metadata and controls
118 lines (101 loc) · 2.75 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
package invitations
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
const invitationFixture = `
{
"id": 1,
"email": "invitee@example.com",
"token": "abc",
"inviter": "autotest",
"created": "2026-03-24T00:00:00Z",
"accepted": false,
"workspace": "wsalpha"
}`
const invitationsFixture = `
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"email": "invitee@example.com",
"token": "abc",
"inviter": "autotest",
"created": "2026-03-24T00:00:00Z",
"accepted": false,
"workspace": "wsalpha"
}
]
}`
const invitationCreateExpected = `{"email":"invitee@example.com"}`
type fakeWorkspaceInvitationServer struct{}
func (f *fakeWorkspaceInvitationServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
if req.URL.Path == "/v2/workspaces/wsalpha/invitations" && req.Method == "GET" {
res.Write([]byte(invitationsFixture))
return
}
if req.URL.Path == "/v2/workspaces/wsalpha/invitations" && req.Method == "POST" {
body, _ := io.ReadAll(req.Body)
if string(body) != invitationCreateExpected {
res.WriteHeader(http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusCreated)
res.Write([]byte(invitationFixture))
return
}
if req.URL.Path == "/v2/workspaces/wsalpha/invitations/abc" && req.Method == "GET" {
res.Write([]byte(invitationFixture))
return
}
if req.URL.Path == "/v2/workspaces/wsalpha/invitations/abc" && req.Method == "DELETE" {
res.WriteHeader(http.StatusNoContent)
return
}
fmt.Printf("Unrecognized URL %s\n", req.URL)
res.WriteHeader(http.StatusNotFound)
}
func TestWorkspaceInvitations(t *testing.T) {
t.Parallel()
server := httptest.NewServer(&fakeWorkspaceInvitationServer{})
defer server.Close()
c, err := drycc.New(false, server.URL, "abc")
if err != nil {
t.Fatal(err)
}
expected := api.WorkspaceInvitation{ID: 1, Email: "invitee@example.com", Token: "abc", Inviter: "autotest", Created: "2026-03-24T00:00:00Z", Accepted: false, Workspace: "wsalpha"}
list, _, err := List(c, "wsalpha", 100)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(list, api.WorkspaceInvitations{expected}) {
t.Fatalf("unexpected list: %#v", list)
}
created, err := Create(c, "wsalpha", "invitee@example.com")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(created, expected) {
t.Fatalf("unexpected create: %#v", created)
}
got, err := Get(c, "wsalpha", "abc")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, expected) {
t.Fatalf("unexpected get: %#v", got)
}
if err := Delete(c, "wsalpha", "abc"); err != nil {
t.Fatal(err)
}
}