Beispiel #1
0
        public static async Task GetProfileDataAsync_GivenAnApplicationUserExistsWithOrganisationFunctionBuyer_ReturnsExpectedClaimList()
        {
            var expectedApplicationUser = ApplicationUserBuilder
                                          .Create()
                                          .WithEmailAddress("*****@*****.**")
                                          .WithFirstName("Bob")
                                          .WithLastName("Smith")
                                          .WithOrganisationFunction(OrganisationFunction.Buyer)
                                          .Build();

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock
            .Setup(r => r.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(expectedApplicationUser);

            var expectedOrganisation = OrganisationBuilder
                                       .Create()
                                       .Build();

            Mock <IOrganisationRepository> organisationRepositoryMock = new Mock <IOrganisationRepository>();

            organisationRepositoryMock
            .Setup(r => r.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(expectedOrganisation);

            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .WithOrganisationRepository(organisationRepositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedApplicationUser.Id)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var expected = new List <(string, string)>
            {
                (Subject, expectedApplicationUser.Id),
                (PreferredUserName, expectedApplicationUser.UserName),
                (JwtRegisteredClaimNames.UniqueName, expectedApplicationUser.UserName),
                (GivenName, expectedApplicationUser.FirstName),
                (FamilyName, expectedApplicationUser.LastName),
                (Name, $"{expectedApplicationUser.FirstName} {expectedApplicationUser.LastName}"),
                (Email, expectedApplicationUser.Email),
                (EmailVerified, expectedApplicationUser.EmailConfirmed.ToString(CultureInfo.CurrentCulture).ToLowerInvariant()),
                (ApplicationClaimTypes.PrimaryOrganisationId, expectedApplicationUser.PrimaryOrganisationId.ToString()),
                (ApplicationClaimTypes.PrimaryOrganisationName, expectedOrganisation.Name),
                (ApplicationClaimTypes.OrganisationFunction, expectedApplicationUser.OrganisationFunction.DisplayName),
                (ApplicationClaimTypes.Ordering, Manage),
                (ApplicationClaimTypes.Organisation, View),
            };

            var actual = profileDataRequestContext.IssuedClaims.Select(item => (item.Type, item.Value));

            actual.Should().BeEquivalentTo(expected);
        }
        public async Task GetProfileDataAsync_GivenUserRepositoryDependency_FindByIdAsyncCalledOnce()
        {
            const string expectedUserId = "TestUserId";

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(null as ApplicationUser);

            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedUserId)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            applicationUserRepositoryMock.Verify(x => x.GetByIdAsync(
                                                     It.Is <string>(param => expectedUserId.Equals(param, StringComparison.Ordinal))),
                                                 Times.Once);
        }
        public async Task GetProfileDataAsync_GivenNoApplicationUserExists_ReturnsEmptyClaimList()
        {
            const string expectedUserId = "TestUserId";

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(null as ApplicationUser);

            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedUserId)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var actual = profileDataRequestContext.IssuedClaims.Select(item => new KeyValuePair <string, string>(item.Type, item.Value));

            actual.Should().BeEmpty();
        }
            static async Task Run()
            {
                var sut = ProfileServiceBuilder
                          .Create()
                          .Build();

                var profileDataRequestContext = ProfileDataRequestContextBuilder
                                                .Create()
                                                .WithSubjectId(null)
                                                .Build();

                await sut.GetProfileDataAsync(profileDataRequestContext);
            }
        public async Task GetProfileDataAsync_GivenAnApplicationUserWithTheName_ReturnsExpectedClaimList(
            string firstname,
            string lastname,
            string expectedName)
        {
            var expectedApplicationUser = ApplicationUserBuilder
                                          .Create()
                                          .WithFirstName(firstname)
                                          .WithLastName(lastname)
                                          .Build();

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(expectedApplicationUser);

            var expectedOrganisation = OrganisationBuilder
                                       .Create()
                                       .Build();

            Mock <IOrganisationRepository> organisationRespositoryMock = new Mock <IOrganisationRepository>();

            organisationRespositoryMock.Setup(r => r.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(expectedOrganisation);


            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .WithOrganisationRepository(organisationRespositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedApplicationUser.Id)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var expected = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(Name, expectedName),
            };

            var actual = profileDataRequestContext.IssuedClaims
                         .Where(item => Name.Equals(item.Type, StringComparison.Ordinal))
                         .Select(item => new KeyValuePair <string, string>(item.Type, item.Value));

            actual.Should().BeEquivalentTo(expected);
        }
Beispiel #6
0
        public static async Task GetProfileDataAsync_GivenApplicationUserWithId_ReturnExpectedClaimList(
            string expectedUserId,
            string expectedUserName,
            params string[] expectedClaimTypes)
        {
            var expectedApplicationUser = ApplicationUserBuilder
                                          .Create()
                                          .WithUserId(expectedUserId)
                                          .WithUsername(expectedUserName)
                                          .WithFirstName(string.Empty)
                                          .WithLastName(string.Empty)
                                          .WithOrganisationFunction(OrganisationFunction.Buyer)
                                          .WithEmailAddress(string.Empty)
                                          .Build();

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock
            .Setup(r => r.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(expectedApplicationUser);

            var expectedOrganisation = OrganisationBuilder
                                       .Create()
                                       .Build();

            Mock <IOrganisationRepository> organisationRepositoryMock = new Mock <IOrganisationRepository>();

            organisationRepositoryMock.Setup(r => r.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(expectedOrganisation);

            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .WithOrganisationRepository(organisationRepositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedApplicationUser.Id)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var actual = profileDataRequestContext.IssuedClaims.Select(item => item.Type);

            actual.Should().BeEquivalentTo(expectedClaimTypes);
        }
Beispiel #7
0
        public static async Task GetProfileDataAsync_GivenAnApplicationUserWithTheName_ReturnsExpectedClaimList(
            string firstName,
            string lastName,
            string expectedName)
        {
            var expectedApplicationUser = ApplicationUserBuilder
                                          .Create()
                                          .WithFirstName(firstName)
                                          .WithLastName(lastName)
                                          .Build();

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock
            .Setup(r => r.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(expectedApplicationUser);

            var expectedOrganisation = OrganisationBuilder
                                       .Create()
                                       .Build();

            Mock <IOrganisationRepository> organisationRepositoryMock = new Mock <IOrganisationRepository>();

            organisationRepositoryMock.Setup(r => r.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(expectedOrganisation);

            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .WithOrganisationRepository(organisationRepositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedApplicationUser.Id)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var expected = new List <KeyValuePair <string, string> >
            {
                new(Name, expectedName),
            };
        public async Task GetProfileDataAsync_GivenApplicationUserWithInvalidPrimaryOrganisationId_ReturnDoesNotIncludePrimaryOrganisationName(
            string organisationFunctionDisplayName,
            params string[] expectedClaimTypes)
        {
            var expectedApplicationUser = ApplicationUserBuilder
                                          .Create()
                                          .WithUsername(string.Empty)
                                          .WithFirstName(string.Empty)
                                          .WithLastName(string.Empty)
                                          .WithEmailAddress(string.Empty)
                                          .WithOrganisationFunction(OrganisationFunction.FromDisplayName(organisationFunctionDisplayName))
                                          .Build();

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(expectedApplicationUser);

            Mock <IOrganisationRepository> organisationRespositoryMock = new Mock <IOrganisationRepository>();

            organisationRespositoryMock.Setup(r => r.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(null as Organisation);


            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .WithOrganisationRepository(organisationRespositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedApplicationUser.Id)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var actual = profileDataRequestContext.IssuedClaims.Select(item => item.Type);

            actual.Should().BeEquivalentTo(expectedClaimTypes);
        }