public async Task Should_exclude_judges_when_no_firstname_set() { var expectedGroup = new Group { Id = _groupId, DisplayName = JudgeGroupName }; DistributedCache.Setup(x => x.GetOrAddAsync(It.IsAny <string>(), It.IsAny <Func <Task <Group> > >())).ReturnsAsync(expectedGroup); _graphQueryResponse.Value.Add(_group); SecureHttpRequest .Setup(x => x.GetAsync(It.IsAny <string>(), _judgesGroup)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(_graphQueryResponse, HttpStatusCode.OK)); var users = new List <User> { new User { Id = "TPTest124", DisplayName = "TP Test", GivenName = "TP", Surname = "Test TP" }, new User { Id = "TPTest124", DisplayName = "TP Test", GivenName = "", Surname = "Test TP" }, new User { Id = "Test123", DisplayName = "T Tester", GivenName = "Test", Surname = "Tester" }, new User { Id = "Test124", DisplayName = "T Test", GivenName = "Tester", Surname = "Test" } }; var directoryObject = new DirectoryObject { AdditionalData = new Dictionary <string, object>() }; directoryObject.AdditionalData.Add("value", JsonConvert.SerializeObject(users)); SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, _accessUri)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(directoryObject, HttpStatusCode.OK)); Service = new UserApi.Services.UserAccountService ( SecureHttpRequest.Object, GraphApiSettings, IdentityServiceApiClient.Object, new Settings { IsLive = false, AdGroup = new AdGroup { Judge = JudgeGroupName } }, DistributedCache.Object ); var response = (await Service.GetJudgesAsync()).ToList(); response.Count.Should().Be(2); response.First().DisplayName.Should().Be("T Test"); response.Last().DisplayName.Should().Be("T Tester"); SecureHttpRequest.Verify(s => s.GetAsync(GraphApiSettings.AccessToken, _accessUri), Times.Once); }
public async Task Should_verify_if_user_already_exists() { SecureHttpRequest.Setup(x => x.PostAsync(It.IsAny <string>(), It.IsAny <StringContent>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("already exist", HttpStatusCode.NotFound)); await Service.AddUserToGroupAsync(user, group); SecureHttpRequest.Verify(s => s.PostAsync(It.IsAny <string>(), It.Is <StringContent>(s => s.ReadAsStringAsync().Result == JsonConvert.SerializeObject(customDirectoryObject)), groupAccessUri), Times.Once); }
public async Task Should_get_group_by_given_id() { var accessUri = $"{GraphApiSettings.GraphApiBaseUri}v1.0/groups/{GroupId}"; var group = new Microsoft.Graph.Group() { Id = GroupId }; SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, accessUri)).ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(group, HttpStatusCode.OK)); var response = await Service.GetGroupByIdAsync(GroupId); response.Should().NotBeNull(); response.Id.Should().Be(GroupId); SecureHttpRequest.Verify(s => s.GetAsync(GraphApiSettings.AccessToken, accessUri), Times.Once); }
public async Task Should_get_group_by_given_id() { var directoryObject = new DirectoryObject() { AdditionalData = new Dictionary <string, object> () }; const string json = @"[ { ""@odata.type"" : ""#microsoft.graph.group"" }, { ""@odata.type"" : ""#microsoft.graph.group"" }, { ""@odata.type"" : ""#microsoft.graph.test"" } ]"; directoryObject.AdditionalData.Add("value", json); SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, accessUri)).ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(directoryObject, HttpStatusCode.OK)); var response = await Service.GetGroupsForUserAsync(UserId); response.Should().NotBeNull(); response.Count.Should().Be(2); SecureHttpRequest.Verify(s => s.GetAsync(GraphApiSettings.AccessToken, accessUri), Times.Once); }
public async Task Should_create_new_user_account_successfully() { var existingUsers = new[] { "existing.user", "existing.user1" }; IdentityServiceApiClient.Setup(x => x.GetUsernamesStartingWithAsync(It.IsAny <string>())) .ReturnsAsync(existingUsers.Select(username => username + Domain)); Filter = $"otherMails/any(c:c eq '{RecoveryEmail.Replace("'", "''")}')"; AzureAdGraphQueryResponse.Value = new List <AzureAdGraphUserResponse>(); SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(AzureAdGraphQueryResponse, HttpStatusCode.OK)); var response = await Service.CreateUserAsync("fName", "lName", RecoveryEmail, false); response.Should().NotBeNull(); response.Username.Should().Be(_newAdUserAccount.Username); response.UserId.Should().Be(_newAdUserAccount.UserId); response.OneTimePassword.Should().Be(_newAdUserAccount.OneTimePassword); SecureHttpRequest.Verify(s => s.GetAsync(It.IsAny <string>(), AccessUri), Times.Once); IdentityServiceApiClient.Verify(i => i.CreateUserAsync(It.IsAny <string>(), "fName", "lName", "fName lName", RecoveryEmail, false), Times.Once); }
public async Task Should_return_empty_for_not_found_status_code() { var expectedGroup = new Group { Id = _groupId, DisplayName = JudgeGroupName }; DistributedCache.Setup(x => x.GetOrAddAsync(It.IsAny <string>(), It.IsAny <Func <Task <Group> > >())).ReturnsAsync(expectedGroup); _graphQueryResponse.Value.Add(_group); SecureHttpRequest .Setup(x => x.GetAsync(It.IsAny <string>(), _judgesGroup)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(_graphQueryResponse, HttpStatusCode.OK)); SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, _accessUri)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(string.Empty, HttpStatusCode.NotFound)); Service = new UserApi.Services.UserAccountService ( SecureHttpRequest.Object, GraphApiSettings, IdentityServiceApiClient.Object, new Settings { IsLive = false, AdGroup = new AdGroup { Judge = JudgeGroupName } }, DistributedCache.Object ); var response = await Service.GetJudgesAsync(); response.Should().BeEmpty(); SecureHttpRequest.Verify(s => s.GetAsync(GraphApiSettings.AccessToken, _accessUri), Times.Once); }
public async Task Should_call_graph_api_two_times_following_nextlink() { var expectedGroup = new Group { Id = _groupId, DisplayName = JudgeGroupName }; DistributedCache.Setup(x => x.GetOrAddAsync(It.IsAny <string>(), It.IsAny <Func <Task <Group> > >())).ReturnsAsync(expectedGroup); _graphQueryResponse.Value.Add(_group); SecureHttpRequest .Setup(x => x.GetAsync(It.IsAny <string>(), _judgesGroup)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(_graphQueryResponse, HttpStatusCode.OK)); var users1 = new List <User> { new User { Id = "Test123", DisplayName = "T Tester", GivenName = "Test", Surname = "Tester" }, new User { Id = "Test124", DisplayName = "T Test", GivenName = "Tester", Surname = "Test" } }; var users2 = new List <User> { new User { Id = "Test123", DisplayName = "T Tester", GivenName = "Test", Surname = "Tester" }, new User { Id = "Test124", DisplayName = "T Test", GivenName = "Tester", Surname = "Test" } }; var directoryObject1 = new DirectoryObject { AdditionalData = new Dictionary <string, object>() }; directoryObject1.AdditionalData.Add("value", JsonConvert.SerializeObject(users1)); directoryObject1.AdditionalData.Add("@odata.nextLink", "someLinkToNextPage"); var directoryObject2 = new DirectoryObject { AdditionalData = new Dictionary <string, object>() }; directoryObject2.AdditionalData.Add("value", JsonConvert.SerializeObject(users2)); SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, _accessUri)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(directoryObject1, HttpStatusCode.OK)); SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, "someLinkToNextPage")) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(directoryObject2, HttpStatusCode.OK)); Service = new UserApi.Services.UserAccountService ( SecureHttpRequest.Object, GraphApiSettings, IdentityServiceApiClient.Object, new Settings { IsLive = false, AdGroup = new AdGroup { Judge = JudgeGroupName } }, DistributedCache.Object ); var response = (await Service.GetJudgesAsync()).ToList(); response.Count.Should().Be(4); SecureHttpRequest.Verify(s => s.GetAsync(GraphApiSettings.AccessToken, _accessUri), Times.Once); SecureHttpRequest.Verify(s => s.GetAsync(GraphApiSettings.AccessToken, "someLinkToNextPage"), Times.Once); }