forked from open-networks/go-msgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User_test.go
224 lines (206 loc) · 6.67 KB
/
User_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package msgraph
import (
"fmt"
"testing"
"time"
)
// GetTestUser returns a valid User instance for testing. Will issue a t.Fatalf if the
// user cannot be loaded
func GetTestUser(t *testing.T) User {
t.Helper()
userToTest, errUserToTest := graphClient.GetUser(msGraphExistingUserPrincipalInGroup)
if errUserToTest != nil {
t.Fatalf("Cannot find user %v for Testing", msGraphExistingUserPrincipalInGroup)
}
return userToTest
}
func TestUser_getTimeZoneChoices(t *testing.T) {
userToTest := GetTestUser(t)
if skipCalendarTests {
t.Skip("Skipping due to missing 'MSGraphExistingCalendarsOfUser' value")
}
tests := []struct {
name string
u User
want []supportedTimeZones
wantErr bool
}{
{
name: "Test all",
u: userToTest,
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.u.getTimeZoneChoices(compileGetQueryOptions([]GetQueryOption{}))
if (err != nil) != tt.wantErr {
t.Errorf("User.getTimeZoneChoices() error = %v, wantErr %v", err, tt.wantErr)
return
}
//t.Errorf("Printing for sure")
})
}
}
func TestUser_ListCalendars(t *testing.T) {
if skipCalendarTests {
t.Skip("Skipping due to missing 'MSGraphExistingCalendarsOfUser' value")
}
// testing for ErrNotGraphClientSourced
notGraphClientSourcedUser := User{ID: "none"}
_, err := notGraphClientSourcedUser.ListCalendars()
if err != ErrNotGraphClientSourced {
t.Errorf("Expected error \"ErrNotGraphClientSourced\", but got: %v", err)
}
// continue with normal tests
userToTest := GetTestUser(t)
var wantedCalendars []Calendar
for _, calendarName := range msGraphExistingCalendarsOfUser {
wantedCalendars = append(wantedCalendars, Calendar{Name: calendarName})
}
tests := []struct {
name string
u User
want Calendars
wantErr bool
}{
{
name: "List All Calendars",
u: userToTest,
want: wantedCalendars,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.u.ListCalendars()
if (err != nil) != tt.wantErr {
t.Errorf("GraphClient.ListUserCalendars() error = %v, wantErr %v", err, tt.wantErr)
return
}
Outer:
for _, searchCalendar := range tt.want {
for _, toCompare := range got {
if searchCalendar.Name == toCompare.Name {
continue Outer
}
}
t.Errorf("GraphClient.ListUserCalendars() = %v, want %v", got, tt.want)
}
})
}
}
func TestUser_ListCalendarView(t *testing.T) {
if skipCalendarTests {
t.Skip("Skipping due to missing 'MSGraphExistingCalendarsOfUser' value")
}
userToTest := GetTestUser(t)
type args struct {
startdate time.Time
enddate time.Time
}
tests := []struct {
name string
u User
args args
wantErr bool
}{
{
name: "Existing User",
u: userToTest,
args: args{startdate: time.Now(), enddate: time.Now().Add(7 * 24 * time.Hour)},
wantErr: false,
}, {
name: "User not initialized by GraphClient",
u: User{UserPrincipalName: "testuser"},
args: args{startdate: time.Now(), enddate: time.Now().Add(7 * 24 * time.Hour)},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.u.ListCalendarView(tt.args.startdate, tt.args.enddate)
//fmt.Println("Got User.ListCalendarview(): ", got)
if (err != nil) != tt.wantErr {
t.Errorf("User.ListCalendarView() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && len(got) == 0 {
t.Errorf("User.ListCalendarView() len is 0, but want > 0, Got: %v", got)
}
})
}
}
func TestUser_String(t *testing.T) {
u := GetTestUser(t)
tt := struct {
name string
u *User
want string
}{
name: "Test user func String",
u: &u,
want: fmt.Sprintf("User(ID: \"%v\", BusinessPhones: \"%v\", DisplayName: \"%v\", GivenName: \"%v\", "+
"Mail: \"%v\", MobilePhone: \"%v\", PreferredLanguage: \"%v\", Surname: \"%v\", UserPrincipalName: \"%v\", "+
"ActivePhone: \"%v\", DirectAPIConnection: %v)",
u.ID, u.BusinessPhones, u.DisplayName, u.GivenName, u.Mail, u.MobilePhone, u.PreferredLanguage, u.Surname,
u.UserPrincipalName, u.activePhone, u.graphClient != nil),
}
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.String(); got != tt.want {
t.Errorf("User.String() = %v, want %v", got, tt.want)
}
})
}
func TestUser_UpdateUser(t *testing.T) {
// testing for ErrNotGraphClientSourced
notGraphClientSourcedUser := User{ID: "none"}
err := notGraphClientSourcedUser.UpdateUser(User{})
if err != ErrNotGraphClientSourced {
t.Errorf("Expected error \"ErrNotGraphClientSourced\", but got: %v", err)
}
// continue with normal tests
testuser := createUnitTestUser(t)
targetedCompanyName := "go-msgraph unit test suite UpdateUser" + randomString(25)
testuser.UpdateUser(User{CompanyName: targetedCompanyName})
getUser, err := graphClient.GetUser(testuser.ID, GetWithSelect("id,userPrincipalName,displayName,givenName,companyName"))
if err != nil {
testuser.DeleteUser()
t.Errorf("Cannot perform User.UpdateUser, error: %v", err)
}
if getUser.CompanyName != targetedCompanyName {
testuser.DeleteUser()
t.Errorf("Performed User.UpdateUser, but CompanyName is still \"%v\" instead of wanted \"%v\"", getUser.CompanyName, targetedCompanyName)
}
err = testuser.DeleteUser()
if err != nil {
t.Errorf("Could not User.DeleteUser() (for %v) after User.UpdateUser tests: %v", testuser, err)
}
}
func TestUser_GetShortName(t *testing.T) {
// test a normal case
testuser := User{UserPrincipalName: "[email protected]"}
if testuser.GetShortName() != "dumpty" {
t.Errorf("user.GetShortName() should return \"dumpty\", but returns: %v", testuser.GetShortName())
}
// test a case that actually should never happen... but we all know murphy
testuser = User{UserPrincipalName: "alice"}
if testuser.GetShortName() != "alice" {
t.Errorf("user.GetShortName() should return \"alice\", but returns: %v", testuser.GetShortName())
}
}
func TestUser_GetFullName(t *testing.T) {
testuser := User{GivenName: "Bob", Surname: "Rabbit"}
wanted := fmt.Sprintf("%v %v", testuser.GivenName, testuser.Surname)
if testuser.GetFullName() != wanted {
t.Errorf("user.GetFullName() should return \"%v\", but returns: \"%v\"", wanted, testuser.GetFullName())
}
}
func TestUser_PrettySimpleString(t *testing.T) {
testuser := User{GivenName: "Bob", Surname: "Rabbit", Mail: "[email protected]", MobilePhone: "+1 23456789"}
wanted := fmt.Sprintf("{ %v (%v) (%v) }", testuser.GetFullName(), testuser.Mail, testuser.GetActivePhone())
if testuser.PrettySimpleString() != wanted {
t.Errorf("user.GetFullName() should return \"%v\", but returns: \"%v\"", wanted, testuser.PrettySimpleString())
}
}