Beispiel #1
0
        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_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();
        }
Beispiel #3
0
        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 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_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();
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        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 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);
        }
Beispiel #9
0
        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_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_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);
        }
Beispiel #12
0
        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 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);
        }
Beispiel #14
0
        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);
        }
Beispiel #15
0
        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);
        }
Beispiel #16
0
        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);
        }
Beispiel #17
0
        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);
        }