-
Notifications
You must be signed in to change notification settings - Fork 4
/
member_test.go
209 lines (180 loc) · 6.11 KB
/
member_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2021, ZeroTier, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIdED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIdENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package ztcentral
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/zerotier/go-ztcentral/pkg/spec"
"github.com/zerotier/go-ztcentral/pkg/testutil"
"github.com/zerotier/go-ztidentity"
)
func TestGetMember(t *testing.T) {
testutil.NeedsToken(t)
c, err := NewClient(testutil.InitTokenFromEnv())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
net, err := c.NewNetwork(ctx, "get-member-network", &spec.Network{})
if err != nil {
t.Fatal(err)
}
defer c.DeleteNetwork(ctx, *net.Config.Id)
if _, err := c.GetMember(ctx, *net.Config.Id, "123456789"); err == nil {
t.Fatal("Tried to get a fake member and succeeded")
}
aliceID := ztidentity.NewZeroTierIdentity()
alice, err := c.CreateAuthorizedMember(ctx, *net.Config.Id, aliceID.IDString(), "alice")
if err != nil {
t.Fatal(err)
}
res, err := c.GetMember(ctx, *net.Config.Id, *alice.NodeId)
if err != nil {
t.Fatal(err)
}
if *res.NetworkId != *net.Config.Id {
t.Fatal("network Id of member was not equivalent")
}
if *res.NodeId != *alice.NodeId {
t.Fatal("member Ids were not equivalent")
}
}
func TestCRUDMembers(t *testing.T) {
testutil.NeedsToken(t)
c, err := NewClient(testutil.InitTokenFromEnv())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
net, err := c.NewNetwork(ctx, "get-members-network", &spec.Network{})
if err != nil {
t.Fatal(err)
}
defer c.DeleteNetwork(ctx, *net.Config.Id)
users := map[string]ztidentity.ZeroTierIdentity{
"awk": ztidentity.NewZeroTierIdentity(),
"bash": ztidentity.NewZeroTierIdentity(),
"cc": ztidentity.NewZeroTierIdentity(),
"dpkg": ztidentity.NewZeroTierIdentity(),
"edlin": ztidentity.NewZeroTierIdentity(),
"finger": ztidentity.NewZeroTierIdentity(),
"gopher": ztidentity.NewZeroTierIdentity(),
}
for name, id := range users {
_, err := c.CreateAuthorizedMember(ctx, *net.Config.Id, id.IDString(), name)
if err != nil {
t.Fatal(err)
}
}
members, err := c.GetMembers(ctx, *net.Config.Id)
if err != nil {
t.Fatal(err)
}
for _, member := range members {
id, ok := users[*member.Name]
if !ok {
t.Fatal("could not find member in pre-populated table")
}
if id.IDString() != *member.NodeId {
t.Fatalf("Ids were not equal for member %q", *member.Name)
}
}
table := map[string]struct {
update func(member *spec.Member)
validate func(member *spec.Member) error
}{
"capabilities": {
update: func(member *spec.Member) {
member.Config.Capabilities = &[]int{0, 1, 2}
},
validate: func(member *spec.Member) error {
if !reflect.DeepEqual(*member.Config.Capabilities, []int{0, 1, 2}) {
return fmt.Errorf("DeepEqual did not succeed on capabilities; was %+v", *member.Config.Capabilities)
}
return nil
},
},
"description": {
update: func(member *spec.Member) {
member.Description = stringp("updated")
},
validate: func(member *spec.Member) error {
if *member.Description != "updated" {
return fmt.Errorf("updated value is not present, is: %+v", *member.Description)
}
return nil
},
},
"ssoexempt": {
update: func(member *spec.Member) {
member.Config.SsoExempt = boolp(true)
},
validate: func(member *spec.Member) error {
if member.Config.SsoExempt == nil || !*member.Config.SsoExempt {
return fmt.Errorf("Expected SsoExempt to be true, got: %+v", member.Config.SsoExempt)
}
return nil
},
},
"ssoexempt_false": {
update: func(member *spec.Member) {
member.Config.SsoExempt = boolp(false)
},
validate: func(member *spec.Member) error {
if member.Config.SsoExempt == nil || *member.Config.SsoExempt {
return fmt.Errorf("Expected SsoExempt to be false, got: %+v", member.Config.SsoExempt)
}
return nil
},
},
}
for _, member := range members {
for testName, harness := range table {
harness.update(member)
updated, err := c.UpdateMember(ctx, *member.NetworkId, *member.NodeId, member)
if err != nil {
t.Fatalf("%q: error updating member: %v", testName, err)
}
if err := harness.validate(updated); err != nil {
t.Fatalf("%q: While validating returned object from update call: %v", testName, err)
}
newMember, err := c.GetMember(ctx, *member.NetworkId, *member.NodeId)
if err != nil {
t.Fatalf("%q: While retrieving updated member from fetch: %v", testName, err)
}
if err := harness.validate(newMember); err != nil {
t.Fatalf("%q: While validating updated member from fetch: %v", testName, err)
}
}
}
}