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 void Should_raise_exception_on_unsuccessful_response() { _secureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("test", HttpStatusCode.Unauthorized)); Assert.ThrowsAsync <IdentityServiceApiException>(() => _client.GetUsernamesStartingWithAsync(UserName)); }
public async Task Should_return_user_names_starting_with_text() { var text = "test'user"; var filter = $"startswith(userPrincipalName,'{text.Replace("'", "''")}')"; _queryUrl += $"?$filter={filter}"; var user = new Microsoft.Graph.User() { UserPrincipalName = "TestUser" }; var azureAdGraphQueryResponse = new AzureAdGraphQueryResponse <Microsoft.Graph.User>() { Value = new List <Microsoft.Graph.User> { user } }; _secureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(azureAdGraphQueryResponse, HttpStatusCode.OK)); var response = await _client.GetUsernamesStartingWithAsync(text); response.Should().NotBeNull(); var users = response.ToList(); users.Count.Should().Be(1); users.First().Should().Be("TestUser"); _secureHttpRequest.Verify(x => x.GetAsync(_graphApiSettings.Object.AccessToken, _queryUrl), Times.Once); }
public async Task Should_return_null_with_no_matching_user_filter() { SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("NotFound", HttpStatusCode.NotFound)); var response = await Service.GetUserByFilterAsync(Filter); response.Should().BeNull(); }
public void Should_raise_IdentityServiceApiException_on_unsuccessful_response_on_update() { _secureHttpRequest.Setup(x => x.PatchAsync(It.IsAny <string>(), It.IsAny <StringContent>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("test", HttpStatusCode.BadRequest)); _passwordService.Setup(x => x.GenerateRandomPasswordWithDefaultComplexity()).Returns("TestPwd"); Assert.ThrowsAsync <IdentityServiceApiException>(() => _client.UpdateUserPasswordAsync(UserName)); }
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 void Should_raise_IdentityServiceApiException_on_unsuccessful_response_on_delete() { _secureHttpRequest.Setup(x => x.DeleteAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("test", HttpStatusCode.Unauthorized)); var exception = Assert.ThrowsAsync <IdentityServiceApiException>(async() => await _client.DeleteUserAsync(UserName)); exception.Should().NotBeNull(); exception.Message.Should().Be("Failed to call API: Unauthorized\r\ntest"); }
public new void Setup() { SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(AzureAdGraphQueryResponse, HttpStatusCode.OK)); _newAdUserAccount = new NewAdUserAccount { Username = "******", UserId = "TestUserId", OneTimePassword = "******" }; IdentityServiceApiClient.Setup(i => i.CreateUserAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).ReturnsAsync(_newAdUserAccount); }
public async Task Should_return_judges_list_successfully() { SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), _judgesGroup)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(_graphQueryResponse, HttpStatusCode.OK)); SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), _judgesTestGroup)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(_graphQueryResponse, HttpStatusCode.OK)); var response = await Service.GetJudgesAsync(); response.Should().NotBeNull(); }
public void Should_return_user_exception_for_other_responses() { const string message = "User not authorised"; SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(message, HttpStatusCode.Unauthorized)); var response = Assert.ThrowsAsync <UserServiceException>(async() => await Service.GetUserByFilterAsync(Filter)); response.Should().NotBeNull(); response.Message.Should().Be($"Failed to search user with filter test: {message}"); response.Reason.Should().Be(message); }
public void Should_return_user_exception_for_other_responses() { const string reason = "User not authorised"; SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(reason, HttpStatusCode.Unauthorized)); var response = Assert.ThrowsAsync <UserServiceException>(async() => await Service.GetGroupByIdAsync(GroupId)); response.Should().NotBeNull(); response.Message.Should().Be($"Failed to get group by id {GroupId}: {reason}"); response.Reason.Should().Be(reason); }
public void Should_throw_user_exception_on_other_responses() { var message = $"Failed to add user {user.Id} to group {group.Id}"; var reason = "Unathorized access"; SecureHttpRequest.Setup(x => x.PostAsync(It.IsAny <string>(), It.IsAny <StringContent>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(reason, HttpStatusCode.Unauthorized)); var response = Assert.ThrowsAsync <UserServiceException>(async() => await Service.AddUserToGroupAsync(user, group)); response.Should().NotBeNull(); response.Message.Should().Be($"{message}: {reason}"); response.Reason.Should().Be(reason); }
public async Task Should_return_user_by_given_filter() { AzureAdGraphQueryResponse.Value.Add(new AzureAdGraphUserResponse() { ObjectId = "2" }); SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(AzureAdGraphQueryResponse, HttpStatusCode.OK)); var response = await Service.GetUserByFilterAsync(Filter); response.Should().NotBeNull(); response.Id.Should().Be(AzureAdGraphUserResponse.ObjectId); response.DisplayName.Should().Be(AzureAdGraphUserResponse.DisplayName); response.GivenName.Should().Be(AzureAdGraphUserResponse.GivenName); response.Surname.Should().Be(AzureAdGraphUserResponse.Surname); response.Mail.Should().BeNull(); response.UserPrincipalName.Should().Be(AzureAdGraphUserResponse.UserPrincipalName); }
public void Should_return_user_exception_for_other_responses() { 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)); const string reason = "User not authorised"; SecureHttpRequest.Setup(x => x.GetAsync(It.IsAny <string>(), _accessUri)) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(reason, HttpStatusCode.Unauthorized)); var response = Assert.ThrowsAsync <UserServiceException>(async() => await Service.GetJudgesAsync()); response.Should().NotBeNull(); response.Message.Should().Be($"Failed to get users for group {_groupId}: {reason}"); response.Reason.Should().Be(reason); }
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_create_user_successfully_and_return_NewAdUserAccount() { var username = "******"; var firstName = "Test"; var lastName = "Tester"; var recoveryEmail = "test'*****@*****.**"; var displayName = $"{firstName} {lastName}"; var user = new { displayName, givenName = firstName, surname = lastName, mailNickname = $"{firstName}.{lastName}".ToLower(), otherMails = new List <string> { recoveryEmail }, accountEnabled = true, userPrincipalName = username, passwordProfile = new { forceChangePasswordNextSignIn = true, password = _defaultPassword } }; var json = JsonConvert.SerializeObject(user); _secureHttpRequest.Setup(x => x.PostAsync(It.IsAny <string>(), It.IsAny <StringContent>(), It.IsAny <string>())) .ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage(new Microsoft.Graph.User(), HttpStatusCode.OK)); _passwordService.Setup(x => x.GenerateRandomPasswordWithDefaultComplexity()).Returns(_defaultPassword); var response = await _client.CreateUserAsync(username, firstName, lastName, displayName, recoveryEmail); response.Should().NotBeNull(); response.OneTimePassword.Should().Be(_defaultPassword); _secureHttpRequest.Verify(x => x.PostAsync(_graphApiSettings.Object.AccessToken, It.Is <StringContent>(s => s.ReadAsStringAsync().Result == json), _queryUrl), 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_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_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); }
public async Task Should_return_empty_when_no_matching_group_by_given_userid() { SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, accessUri)).ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("Not found", HttpStatusCode.NotFound)); var response = await Service.GetGroupsForUserAsync(UserId); response.Should().BeEmpty(); }
public async Task Should_return_null_when_no_matching_group_by_given_id() { var accessUri = $"{GraphApiSettings.GraphApiBaseUri}v1.0/groups/{GroupId}"; SecureHttpRequest.Setup(s => s.GetAsync(GraphApiSettings.AccessToken, accessUri)).ReturnsAsync(ApiRequestHelper.CreateHttpResponseMessage("Not found", HttpStatusCode.NotFound)); var response = await Service.GetGroupByIdAsync(GroupId); response.Should().BeNull(); }
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); }