-
Notifications
You must be signed in to change notification settings - Fork 5
/
memberships_test.go
137 lines (105 loc) · 3.18 KB
/
memberships_test.go
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
135
136
137
package redmine
import (
"os"
"strconv"
"testing"
)
func TestMembershipCRUD(t *testing.T) {
var r Context
// Get env variables
testMembershipRoleID1, err := strconv.ParseInt(os.Getenv("REDMINE_ROLE_ID_1"), 10, 64)
if err != nil {
t.Fatal("Membership test error: env variable `REDMINE_ROLE_ID_1` is incorrect")
}
testMembershipRoleID2, err := strconv.ParseInt(os.Getenv("REDMINE_ROLE_ID_2"), 10, 64)
if err != nil {
t.Fatal("Membership test error: env variable `REDMINE_ROLE_ID_2` is incorrect")
}
if testMembershipRoleID1 == 0 || testMembershipRoleID2 == 0 {
t.Fatal("Membership test error: env variables `REDMINE_ROLE_ID_1` or `REDMINE_ROLE_ID_2` does not set")
}
// Init Redmine context
initTest(&r, t)
// Preparing auxiliary data
uCreated := testUserCreate(t, r)
defer testUserDetele(t, r, uCreated.ID)
pCreated := testProjectCreate(t, r, []int64{})
defer testProjectDetele(t, r, pCreated.Identifier)
// Add and delete
mCreated := testMembershipAdd(t, r, pCreated.Identifier, uCreated.ID, testMembershipRoleID1)
defer testMembershipDetele(t, r, mCreated.ID)
// Get all
testMembershipAllGet(t, r, mCreated.ID, pCreated.Identifier, testMembershipRoleID1)
// Update
testMembershipUpdate(t, r, mCreated.ID, testMembershipRoleID1, testMembershipRoleID2)
// Get single (check role `testMembershipRoleID2` exists in specified membership)
testMembershipSingleGet(t, r, mCreated.ID, testMembershipRoleID2)
}
func testMembershipAdd(t *testing.T, r Context, projectID string, userID, roleID int64) MembershipObject {
m, _, err := r.MembershipAdd(
projectID,
MembershipAdd{
Membership: MembershipAddObject{
UserID: userID,
RoleIDs: []int64{roleID},
},
},
)
if err != nil {
t.Fatal("Membership add error:", err)
}
t.Logf("Membership add: success")
return m
}
func testMembershipUpdate(t *testing.T, r Context, id, roleID1, roleID2 int64) {
_, err := r.MembershipUpdate(
id,
MembershipUpdate{
Membership: MembershipUpdateObject{
RoleIDs: []int64{roleID1, roleID2},
},
},
)
if err != nil {
t.Fatal("Membership update error:", err)
}
t.Logf("Membership update: success")
}
func testMembershipDetele(t *testing.T, r Context, id int64) {
_, err := r.MembershipDelete(id)
if err != nil {
t.Fatal("Membership delete error:", err)
}
t.Logf("Membership delete: success")
}
func testMembershipAllGet(t *testing.T, r Context, id int64, projectID string, roleID int64) {
m, _, err := r.MembershipAllGet(projectID)
if err != nil {
t.Fatal("Memberships get error:", err)
}
for _, e := range m.Memberships {
if e.ID == id {
for _, role := range e.Roles {
if role.ID == roleID {
t.Logf("Memberships get: success")
return
}
}
t.Fatal("Memberships get error: can't find role in added membership")
}
}
t.Fatal("Memberships get error: can't find added membership")
}
func testMembershipSingleGet(t *testing.T, r Context, id, roleID int64) {
m, _, err := r.MembershipSingleGet(id)
if err != nil {
t.Fatal("Membership get error:", err)
}
for _, role := range m.Roles {
if role.ID == roleID {
t.Logf("Membership get: success")
return
}
}
t.Fatal("Membership get error: can't find role in added membership")
}