public void MerchantAggregate_AddSecurityUserToMerchant_MerchantNotCreated_ErrorThrown() { MerchantAggregate aggregate = MerchantAggregate.Create(TestData.MerchantId); InvalidOperationException exception = Should.Throw <InvalidOperationException>(() => { aggregate.AddSecurityUser(TestData.SecurityUserId, TestData.EstateUserEmailAddress); }); exception.Message.ShouldContain("Merchant has not been created"); }
public async Task <Guid> CreateMerchantUser(Guid estateId, Guid merchantId, String emailAddress, String password, String givenName, String middleName, String familyName, CancellationToken cancellationToken) { MerchantAggregate merchantAggregate = await this.MerchantAggregateRepository.GetLatestVersion(merchantId, cancellationToken); // Check merchant has been created if (merchantAggregate.IsCreated == false) { throw new InvalidOperationException($"Merchant Id {merchantId} has not been created"); } // Estate Id is a valid estate EstateAggregate estateAggregate = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); if (estateAggregate.IsCreated == false) { throw new InvalidOperationException($"Estate Id {estateId} has not been created"); } CreateUserRequest createUserRequest = new CreateUserRequest { EmailAddress = emailAddress, FamilyName = familyName, GivenName = givenName, MiddleName = middleName, Password = password, PhoneNumber = "123456", // Is this really needed :| Roles = new List <String>(), Claims = new Dictionary <String, String>() }; String merchantRoleName = Environment.GetEnvironmentVariable("MerchantRoleName"); createUserRequest.Roles.Add(String.IsNullOrEmpty(merchantRoleName) ? "Merchant" : merchantRoleName); createUserRequest.Claims.Add("estateId", estateId.ToString()); createUserRequest.Claims.Add("merchantId", merchantId.ToString()); CreateUserResponse createUserResponse = await this.SecurityServiceClient.CreateUser(createUserRequest, cancellationToken); // Add the user to the aggregate merchantAggregate.AddSecurityUser(createUserResponse.UserId, emailAddress); // TODO: add a delete user here in case the aggregate add fails... await this.MerchantAggregateRepository.SaveChanges(merchantAggregate, cancellationToken); return(createUserResponse.UserId); }
public void MerchantAggregate_AddSecurityUserToMerchant_SecurityUserIsAdded() { MerchantAggregate aggregate = MerchantAggregate.Create(TestData.MerchantId); aggregate.Create(TestData.EstateId, TestData.MerchantName, TestData.DateMerchantCreated); aggregate.AddSecurityUser(TestData.SecurityUserId, TestData.MerchantUserEmailAddress); Merchant merchantModel = aggregate.GetMerchant(); merchantModel.SecurityUsers.ShouldHaveSingleItem(); SecurityUser securityUser = merchantModel.SecurityUsers.Single(); securityUser.EmailAddress.ShouldBe(TestData.MerchantUserEmailAddress); securityUser.SecurityUserId.ShouldBe(TestData.SecurityUserId); }