public static async Task <Profile> Save(this Profile profile, ILogger logger = null, Account account = null) { var address = ApiLocation.AccountProfile; // If account is null then this will be invoked with a new account // This is a one-time usage for testing because the caller will not have access // to the account context for any additional calls // If additional calls are required for the same account context then pass an account in and reuse it var identity = ClaimsIdentityFactory.Build(account, profile); await Client.Put(address, logger, profile, identity, HttpStatusCode.NoContent).ConfigureAwait(false); var actual = await Client.Get <Profile>(address, logger, identity).ConfigureAwait(false); if (profile.BannedAt != null) { var profileUri = ApiLocation.ProfileFor(actual.Id); var administrator = ClaimsIdentityFactory.Build().AsAdministrator(); // Use an admin to cancel the profile await Client.Delete(profileUri, logger, administrator).ConfigureAwait(false); actual.BannedAt = profile.BannedAt; } return(actual); }
public async Task DeleteReturnsNotFoundForEmptyIdTest() { var administrator = ClaimsIdentityFactory.Build().AsAdministrator(); var profileUri = ApiLocation.ProfileFor(Guid.Empty); await Client.Delete(profileUri, _logger, administrator, HttpStatusCode.NotFound).ConfigureAwait(false); }
public async Task GetReturnsNotFoundForInvalidIdTest() { var profile = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>(); var address = ApiLocation.ProfileFor(profile.Id); await Client.Get(address, _logger, null, HttpStatusCode.NotFound).ConfigureAwait(false); }
public async Task GetReturnsSkillAfterApprovedForAnonymousUserTest() { var account = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Account>(); var profile = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().ClearCategories(); profile = await profile.Save(_logger, account).ConfigureAwait(false); var newSkill = new Skill { Level = SkillLevel.Expert, Name = Guid.NewGuid().ToString("N") }; profile.Skills.Add(newSkill); profile = await profile.Save(_logger, account).ConfigureAwait(false); var profileAddress = ApiLocation.ProfileFor(profile.Id); var firstActual = await Client.Get <PublicProfile>(profileAddress, _logger).ConfigureAwait(false); firstActual.Skills.Should().NotContain(x => x.Name == newSkill.Name); var administrator = ClaimsIdentityFactory.Build().AsAdministrator(); await new NewCategory { Group = CategoryGroup.Skill, Name = newSkill.Name }.Save(_logger, administrator).ConfigureAwait(false); var secondActual = await Client.Get <PublicProfile>(profileAddress, _logger).ConfigureAwait(false); secondActual.Skills.Should().Contain(x => x.Name == newSkill.Name); }
public async Task GetOnlyIncludesApprovedSkillTest(bool approved) { var categoryName = Guid.NewGuid().ToString(); if (approved) { // Store the category as an administrator which will also make it approved by default var category = new NewCategory { Group = CategoryGroup.Skill, Name = categoryName }; await category.Save(_logger).ConfigureAwait(false); } var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>() .Set(x => x.Skills.First().Name = categoryName) .Save(_logger).ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); var actual = await Client.Get <PublicProfile>(address, _logger).ConfigureAwait(false); if (approved) { actual.Skills.Should().Contain(x => x.Name == categoryName); } else { actual.Skills.Should().NotContain(x => x.Name == categoryName); } }
public async Task PutUpdatesProfileInformationForBannedAccountTest() { var expected = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <UpdatableProfile>(); var user = ClaimsIdentityFactory.Build(null, expected); // Create the profile await Client.Put(ApiLocation.AccountProfile, _logger, expected, user, HttpStatusCode.NoContent) .ConfigureAwait(false); // Get the profile var storedProfile = await Client.Get <Profile>(ApiLocation.AccountProfile, _logger, user).ConfigureAwait(false); var administrator = ClaimsIdentityFactory.Build().AsAdministrator(); // Ban the profile await Client.Delete(ApiLocation.ProfileFor(storedProfile.Id), _logger, administrator).ConfigureAwait(false); // Make a change to the profile expected.FirstName = Guid.NewGuid().ToString(); // Update the profile again await Client.Put(ApiLocation.AccountProfile, _logger, expected, user, HttpStatusCode.NoContent) .ConfigureAwait(false); var actual = await Client.Get <Profile>(ApiLocation.AccountProfile, _logger, user).ConfigureAwait(false); actual.Should().BeEquivalentTo(expected, opt => opt.ExcludingMissingMembers()); }
public async Task DeleteReturnsUnauthorizedForAnonymousUserTest() { var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().Save().ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); await Client.Delete(address, _logger, null, HttpStatusCode.Unauthorized).ConfigureAwait(false); }
public async Task DeleteReturnsForbiddenWhenUserNotAdministratorTest() { var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().Save().ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); var identity = ClaimsIdentityFactory.Build(); await Client.Delete(address, _logger, identity, HttpStatusCode.Forbidden).ConfigureAwait(false); }
public async Task GetReturnsNotFoundForHiddenProfileTest() { var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>() .Set(x => x.Status = ProfileStatus.Hidden).Save(_logger).ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); await Client.Get(address, _logger, null, HttpStatusCode.NotFound).ConfigureAwait(false); }
public async Task GetReturnsNotFoundForBannedProfileTest() { var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>() .Set(x => x.BannedAt = DateTimeOffset.UtcNow).Save(_logger).ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); await Client.Get(address, _logger, null, HttpStatusCode.NotFound).ConfigureAwait(false); }
public async Task GetDoesNotReturnProfileEmailTest() { var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().Save().ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); var actual = await Client.Get <Profile>(address, _logger).ConfigureAwait(false); actual.Email.Should().BeNull(); }
public async Task GetReturnsOkForAnonymousUserWhenSkillsIsNullTest() { var profile = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().Set(x => x.Skills = null); await profile.SaveAllCategories().ConfigureAwait(false); profile = await profile.Save().ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); var actual = await Client.Get <PublicProfile>(address, _logger).ConfigureAwait(false); actual.Should().BeEquivalentTo(profile, opt => opt.ExcludingMissingMembers()); }
public async Task DeleteBansAccountTest() { var account = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Account>(); var accountIdentity = ClaimsIdentityFactory.Build(account); var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().Save(_logger, account) .ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); var identity = ClaimsIdentityFactory.Build().AsAdministrator(); await Client.Delete(address, _logger, identity).ConfigureAwait(false); var actual = await Client.Get <Profile>(ApiLocation.AccountProfile, _logger, accountIdentity) .ConfigureAwait(false); actual.BannedAt.Should().BeCloseTo(DateTimeOffset.UtcNow, 5000); }
public async Task DeleteRemovesAccountFromPublicViewTest() { var profile = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>(); var account = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Account>(); var identity = ClaimsIdentityFactory.Build(account, profile); await profile.SaveAllCategories(_logger).ConfigureAwait(false); profile = await profile.Save(_logger, account).ConfigureAwait(false); var address = ApiLocation.AccountProfile; await Client.Delete(address, _logger, identity).ConfigureAwait(false); var profileAddress = ApiLocation.ProfileFor(profile.Id); await Client.Get <PublicProfile>(profileAddress, _logger, null, HttpStatusCode.NotFound).ConfigureAwait(false); await IsDeleted(profile.Id, identity).ConfigureAwait(false); }
public async Task GetReturnsCategoryCorrectlyWhenToggledBetweenVisibleAndInvisibleTest() { var profile = Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>().ClearCategories() .Set(x => x.Gender = Guid.NewGuid().ToString()); await profile.SaveAllCategories().ConfigureAwait(false); profile = await profile.Save().ConfigureAwait(false); var address = ApiLocation.ProfileFor(profile.Id); var firstActual = await Client.Get <PublicProfile>(address, _logger).ConfigureAwait(false); firstActual.Gender.Should().Be(profile.Gender); var administrator = ClaimsIdentityFactory.Build().AsAdministrator(); var categoryAddress = ApiLocation.Category(CategoryGroup.Gender, profile.Gender); var updateCategory = new UpdateCategory { Visible = false }; // Hide the gender category await Client.Put(categoryAddress, _logger, updateCategory, administrator, HttpStatusCode.NoContent) .ConfigureAwait(false); var secondActual = await Client.Get <PublicProfile>(address, _logger).ConfigureAwait(false); secondActual.Gender.Should().BeNullOrEmpty(); updateCategory.Visible = true; // Show the gender category again await Client.Put(categoryAddress, _logger, updateCategory, administrator, HttpStatusCode.NoContent) .ConfigureAwait(false); var thirdActual = await Client.Get <PublicProfile>(address, _logger).ConfigureAwait(false); thirdActual.Gender.Should().Be(profile.Gender); }
public async Task DeleteReturnsNotFoundWhenProfileAlreadyBannedTest() { var account = Model.Create <Account>(); var profile = await Model.UsingBuildStrategy <ProfileBuildStrategy>().Create <Profile>() .Save(_logger, account) .ConfigureAwait(false); var newCategory = profile.Skills.First(); var address = ApiLocation.Categories; var categoryApproval = new NewCategory { Group = CategoryGroup.Skill, Name = newCategory.Name }; var administrator = ClaimsIdentityFactory.Build().AsAdministrator(); var profileAddress = ApiLocation.ProfileFor(profile.Id); await Client.Post(address, _logger, categoryApproval, administrator).ConfigureAwait(false); var firstActual = await Client.Get <List <PublicCategory> >(address, _logger).ConfigureAwait(false); firstActual.Single(x => x.Group == CategoryGroup.Skill && x.Name == newCategory.Name).LinkCount.Should() .Be(1); await Client.Delete(profileAddress, _logger, administrator).ConfigureAwait(false); var secondActual = await Client.Get <List <PublicCategory> >(address, _logger).ConfigureAwait(false); secondActual.Single(x => x.Group == CategoryGroup.Skill && x.Name == newCategory.Name).LinkCount.Should() .Be(0); await Client.Delete(profileAddress, _logger, administrator, HttpStatusCode.NotFound).ConfigureAwait(false); var thirdActual = await Client.Get <List <PublicCategory> >(address, _logger).ConfigureAwait(false); thirdActual.Single(x => x.Group == CategoryGroup.Skill && x.Name == newCategory.Name).LinkCount.Should() .Be(0); }