-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphAPITests.cs
More file actions
274 lines (234 loc) · 9.57 KB
/
Copy pathGraphAPITests.cs
File metadata and controls
274 lines (234 loc) · 9.57 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph.Models;
using Microsoft.Graph;
using Moq;
using Xunit;
using Azure.Identity;
namespace API.Library.Test
{
public class GraphAPITests :IDisposable
{
private readonly Mock<IGraphClientWrapper> graphClientWrapperMock;
private readonly Mock<IApiConfiguration> apiConfigurationMock;
private readonly Dictionary<string, string> settingsMock;
private readonly Mock<ICacheD> cacheDMock;
private GraphAPI graphApi;
public GraphAPITests()
{
// Mock IApiConfiguration and Settings
apiConfigurationMock = new Mock<IApiConfiguration>();
settingsMock = new Dictionary<string, string>
{
{ "API_AUTHENTICATION_TYPE", "ANY_ENTRAID" },
{ "API_STATELESS", "false" },
{ "API_MASK_PARAMETERS", "MtrInput,Lgn1Fa,Totp" },
{ "API_ENTRAID_EXTERNAL_CALL_TIMEOUT", "10" },
{ "API_ENTRAID_CUSTOM_PROPERTIES", "" },
{ "API_ENTRAID_GROUPS", "LAP_Allow_CSO_Staff_Access,LAP_Allow_Field_Interviewer_Staff_Access,LAP_Allow_Field_Coordinator_Staff_Access" },
{ "API_ENTRAID_TENANTID", "e7f948fc-4a62-4c63-8b00-03aa6a169e55" },
{ "API_ENTRAID_CLIENTID", "b3f8c5d2-1e4a-4b6c-9f0e-7c8d5e6f7a8b" },
{ "API_ENTRAID_CLIENTSECRET", "test-secret" }
};
apiConfigurationMock.SetupGet(ac => ac.Settings).Returns(settingsMock);
// Mock CacheD and Get_BSO
cacheDMock = new Mock<ICacheD>();
var memCachedDValueMock = new MemCachedD_Value
{
hasData = false
};
cacheDMock.Setup(c => c.Get_BSO_WITHLOCK<dynamic>("API", "EntraID", "GetGroupMembers", It.IsAny<string>()))
.Returns(memCachedDValueMock);
ApiServicesHelper.CacheD = cacheDMock.Object;
// Mock the wrapper interface instead of GraphServiceClient
graphClientWrapperMock = new Mock<IGraphClientWrapper>();
// Set up ApiServicesHelper.GraphClientWrapper to use our mock
ApiServicesHelper.GraphClientWrapper = graphClientWrapperMock.Object;
// Use the default constructor
graphApi = new GraphAPI();
}
[Fact]
public void GetGroupMembers_ReturnsUserDetails_WhenSuccessful()
{
// Arrange
var fakeUsers = new List<User>
{
new User
{
Id = "1",
DisplayName = "John Doe",
JobTitle = "Developer",
Department = "Engineering",
GivenName = "John",
Surname = "Doe",
OnPremisesSamAccountName = "johndoe",
OnPremisesDistinguishedName = "CN=John Doe,OU=Users,DC=example,DC=com",
AccountEnabled = true,
BusinessPhones = new List<string> { "555-1234" }
}
};
// Mock the wrapper to return our fake users
graphClientWrapperMock
.Setup(x => x.GetGroupMembersAsync(It.IsAny<string[]>()))
.Returns(Task.FromResult(fakeUsers));
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var result = graphApi.GetGroupMembers();
// Assert
Assert.NotNull(result);
Assert.IsType<Dictionary<string, UserDetail>>(result);
}
[Fact]
public void GetGroupMembers_ReturnsEmptyDictionary_WhenNoUsersExist()
{
// Arrange
// Mock empty result from wrapper
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var result = graphApi.GetGroupMembers();
// Assert
Assert.NotNull(result);
Assert.IsType<Dictionary<string, UserDetail>>(result);
}
[Fact]
public void GetGroupMembers_ReturnsEmptyDictionary_OnException()
{
// Arrange
// Mock the wrapper to throw an exception
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var result = graphApi.GetGroupMembers();
// Assert
Assert.NotNull(result);
Assert.IsType<Dictionary<string, UserDetail>>(result);
Assert.Empty(result);
}
[Fact]
public void GetUserByEmail_ReturnsUserDetail_WhenUserExists()
{
// Arrange
var expectedUser = new User
{
Id = "2",
DisplayName = "Jane Smith",
JobTitle = "Manager",
Department = "HR",
GivenName = "Jane",
Surname = "Smith",
OnPremisesSamAccountName = "janesmith",
OnPremisesDistinguishedName = "CN=Jane Smith,OU=Users,DC=example,DC=com",
AccountEnabled = true,
BusinessPhones = new List<string> { "555-5678" }
};
// Mock the wrapper method
graphClientWrapperMock
.Returns(Task.FromResult(expectedUser));
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
// Assert
Assert.NotNull(result);
Assert.IsType<UserDetail>(result);
Assert.Equal("Jane Smith", result.DisplayName);
Assert.Equal("Manager", result.JobTitle);
}
[Fact]
public void GetUserByEmail_ReturnsNull_WhenUserNotFound()
{
// Arrange
graphClientWrapperMock
.Returns(Task.FromResult<User>(null));
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
// Assert
Assert.Null(result);
}
[Fact]
public void GetUserByEmail_ReturnsNull_OnException()
{
// Arrange
graphClientWrapperMock
.Throws(new Exception("User not found"));
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
// Assert
Assert.Null(result);
}
[Fact]
public void Search_ReturnsUserDetail_WhenUserExists()
{
// Arrange
// This test depends on GetGroupMembers, so you might need to mock that method's dependencies
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var result = graphApi.Search(testUpn);
// Assert based on whether the user exists in the mocked data
// This will depend on your mock setup for GetGroupMembers
}
[Fact]
public void Search_ReturnsNull_WhenUserNotFound()
{
// Arrange
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var result = graphApi.Search(nonExistentUpn);
// Assert
Assert.Null(result);
}
[Fact]
public void GetGraphClient_ReturnsSameInstance_WhenCalledMultipleTimes()
{
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var client1 = graphApi.getGraphClient();
var client2 = graphApi.getGraphClient();
// Assert
Assert.NotNull(client1);
Assert.NotNull(client2);
Assert.Same(client1, client2);
}
[Fact]
public void GetGraphClient_ReturnsValidInstance()
{
// Act
ApiServicesHelper.ApiConfiguration = apiConfigurationMock.Object;
var client = graphApi.getGraphClient();
// Assert
Assert.NotNull(client);
Assert.IsType<GraphServiceClient>(client);
}
public void Dispose()
{
ResetApiServicesHelper();
}
private void ResetApiServicesHelper()
{
// Clear any static configuration between tests
ApiServicesHelper.ApiConfiguration = null;
ApiServicesHelper.CacheD = null;
ApiServicesHelper.GraphClientWrapper = null;
apiConfigurationMock.Reset();
graphClientWrapperMock.Reset();
settingsMock.Clear();
cacheDMock.Reset();
graphApi = null;
}
}
}