public void ApplyingNoMigrationsShouldDoNothing() { var testSubject = new ChangePlan(Do.Apply, new int[] {}); testSubject.ApplyTo(this._database, this._definedMigrations.ToLoaders()); this._database.AppliedMigrations.Should().BeEmpty(); this._database.CommittedTheChanges.Should().BeFalse(); }
public void CanChangePlan_should_not_throw_exception_if_same_user_but_other_plan() { var command = new ChangePlan { PlanId = "free", Actor = new RefToken("user", "me") }; var plan = new AppPlan(new RefToken("user", "me"), "premium"); GuardApp.CanChangePlan(command, plan, appPlans); }
public void CanChangePlan_should_throw_exception_if_plan_is_the_same() { var command = new ChangePlan { PlanId = "free", Actor = new RefToken("user", "me") }; var plan = new AppPlan(new RefToken("user", "me"), "free"); Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void CanChangePlan_should_not_throw_exception_if_same_user_but_other_plan() { var command = new ChangePlan { PlanId = "basic", Actor = RefToken.User("me") }; var plan = new AppPlan(command.Actor, "premium"); GuardApp.CanChangePlan(command, App(plan), appPlans); }
public void CanChangePlan_should_throw_exception_if_plan_id_null() { var command = new ChangePlan { Actor = new RefToken("user", "me") }; AppPlan plan = null; Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void CanChangePlan_should_throw_exception_if_plan_id_is_null() { var command = new ChangePlan { Actor = new RefToken("user", "me") }; AppPlan?plan = null; ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("Plan id is required.", "PlanId")); }
private void ChangePlan(ChangePlan command) { if (string.Equals(appPlansProvider.GetFreePlan()?.Id, command.PlanId, StringComparison.Ordinal)) { Raise(command, new AppPlanReset()); } else { Raise(command, new AppPlanChanged()); } }
public void CanChangePlan_should_throw_exception_if_plan_is_the_same() { var command = new ChangePlan { PlanId = "basic", Actor = new RefToken("user", "me") }; var plan = new AppPlan(command.Actor, "basic"); ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("App has already this plan.")); }
public void ApplyingMigrationShouldNotifyUser() { var testSubject = new ChangePlan(Do.Apply, new[] {3, 4}); testSubject.ApplyTo(new DatabaseLocalMemory(), TestData.Migrations(3, 4).ToLoaders()); this._messagesSentToUser.Should().ContainInOrder(new[] { "Applying version 3 to the database.", "Applying version 4 to the database." }); }
public void UnapplyingMigrationShouldNotifyUser() { var testSubject = new ChangePlan(Do.Unapply, new[] {5, 4}); testSubject.ApplyTo(new DatabaseLocalMemory(), TestData.Migrations(4, 5).ToLoaders()); this._messagesSentToUser.Should().ContainInOrder(new[] { "Unapplying version 5 from the database.", "Unapplying version 4 from the database.", }); }
public AppDomainObject ChangePlan(ChangePlan command) { Guard.Valid(command, nameof(command), () => "Cannot change plan"); ThrowIfNotCreated(); ThrowIfOtherUser(command); RaiseEvent(SimpleMapper.Map(command, new AppPlanChanged())); return(this); }
public async Task Should_not_override_app_id() { var command = new ChangePlan { AppId = Guid.NewGuid() }; var context = Ctx(command); await sut.HandleAsync(context); Assert.NotEqual(appId.Id, command.AppId); }
public void CanChangePlan_should_throw_exception_if_plan_was_configured_from_another_user() { var command = new ChangePlan { PlanId = "basic", Actor = new RefToken("user", "me") }; var plan = new AppPlan(new RefToken("user", "other"), "premium"); ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("Plan can only changed from the user who configured the plan initially.")); }
public void CanChangePlan_should_throw_exception_if_plan_not_found() { var command = new ChangePlan { PlanId = "notfound", Actor = new RefToken("user", "me") }; AppPlan?plan = null; ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("A plan with this id does not exist.", "PlanId")); }
public void ChangePlan(ChangePlan command) { if (string.Equals(appPlansProvider.GetFreePlan()?.Id, command.PlanId)) { RaiseEvent(SimpleMapper.Map(command, new AppPlanReset())); } else { RaiseEvent(SimpleMapper.Map(command, new AppPlanChanged())); } }
private void ThrowIfOtherUser(ChangePlan command) { if (!string.IsNullOrWhiteSpace(command.PlanId) && planOwner != null && !planOwner.Equals(command.Actor)) { throw new ValidationException("Plan can only be changed from current user."); } if (string.Equals(command.PlanId, planId, StringComparison.OrdinalIgnoreCase)) { throw new ValidationException("App has already this plan."); } }
public void UnapplyingMigrationShouldNotifyUser() { var testSubject = new ChangePlan(Do.Unapply, new[] { 5, 4 }); testSubject.ApplyTo(new DatabaseLocalMemory(), TestData.Migrations(4, 5).ToLoaders()); _messagesSentToUser.Should().ContainInOrder(new[] { "Unapplying version 5 from the database.", "Unapplying version 4 from the database." }); }
public void CanChangePlan_should_throw_exception_if_plan_not_found() { A.CallTo(() => appPlans.GetPlan("free")) .Returns(null); var command = new ChangePlan { PlanId = "free", Actor = new RefToken("user", "me") }; AppPlan plan = null; Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void ShouldGiveGoodErrorWhenAttemptToApplyUndefinedMigration() { var testSubject = new ChangePlan(Do.Unapply, new[] {19}); Action application = () => testSubject.ApplyTo(this._database, this._definedMigrations.ToLoaders()); application.ShouldThrow<TerminateProgramWithMessageException>().WithMessage( @"Missing migration 19 I needed to Unapply migration 19, but could not find a definition for it. Please make sure there is a file in your migration directory named '19_some_name.migration.sql'." ).And.ErrorLevel.Should().Be(1); }
public async Task <Result <SuccessfulChangePlan> > ChangePlan(ChangePlan request, AccessToken accessToken = default, CancellationToken cancellationToken = default) { var nameValues = new NameValueCollection(); nameValues.Add("api-version", "2020-09-09"); var fullPath = path + "/change-plan"; var requestUri = GetUri(fullPath, nameValueCollection: nameValues); return(await HttpPut <SuccessfulChangePlan>(requestUri, data : request, administrationOrApiKey : AdministrationKey, token : accessToken, cancellationToken : cancellationToken)); }
public void ShouldGiveGoodErrorWhenAttemptToApplyUndefinedMigration() { var testSubject = new ChangePlan(Do.Unapply, new[] { 19 }); Action application = () => testSubject.ApplyTo(_database, _definedMigrations.ToLoaders()); application.Should().Throw <TerminateProgramWithMessageException>().WithMessage( @"Missing migration 19 I needed to Unapply migration 19, but could not find a definition for it. Please make sure there is a file in your migration directory named '19_some_name.migration.sql'." ).And.ErrorLevel.Should().Be(1); }
public async Task <IActionResult> ChangeRoleAysnc([FromBody] ChangePlan roleChanged) { var accountId = User.ReadClaimAsGuidValue("urn:codefliptodo:accountid"); roleChanged.AccountId = accountId; var mediator = await _mediator.Send(roleChanged); if (mediator == false) { return(BadRequest()); } return(Ok()); }
public async Task ChangePlan_should_not_call_billing_manager_for_callback() { var command = new ChangePlan { PlanId = planIdPaid, FromCallback = true }; await ExecuteCreateAsync(); var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(new EntitySavedResult(5)); A.CallTo(() => appPlansBillingManager.ChangePlanAsync(Actor.Identifier, AppNamedId, planIdPaid)) .MustNotHaveHappened(); }
public async Task ChangePlan_should_not_make_update_for_redirect_result() { var command = new ChangePlan { PlanId = planIdPaid }; A.CallTo(() => appPlansBillingManager.ChangePlanAsync(Actor.Identifier, AppNamedId, planIdPaid)) .Returns(new RedirectToCheckoutResult(new Uri("http://squidex.io"))); await ExecuteCreateAsync(); var result = await sut.ExecuteAsync(CreateCommand(command)); result.ShouldBeEquivalent(new RedirectToCheckoutResult(new Uri("http://squidex.io"))); Assert.Null(sut.Snapshot.Plan); }
protected Task On(ChangePlan command, CommandContext context) { if (!appPlansProvider.IsConfiguredPlan(command.PlanId)) { var error = new ValidationError($"The plan '{command.PlanId}' does not exists", nameof(CreateApp.Name)); throw new ValidationException("Cannot change plan", error); } return(handler.UpdateAsync <AppDomainObject>(context, async a => { a.ChangePlan(command); await appPlansBillingManager.ChangePlanAsync(command.Actor.Identifier, a.Id, a.Name, command.PlanId); })); }
public async Task <IActionResult> ChangeSubscription([FromBody] ChangeSubscription changeSubscriptionCommand) { var accountId = User.ReadClaimAsGuidValue("urn:codefliptodo:accountid"); var account = await _accountRepository.FindAccountByIdAsync(accountId); if (account.PaymentMethodId == null) { return(BadRequest()); } var createSubscriptionCommand = new CreateSubscription { AccountId = accountId, Plan = "Free", }; var createSubscriptionResult = await _mediator.Send(createSubscriptionCommand); if (createSubscriptionResult) { changeSubscriptionCommand.AccountId = accountId; var changeSubscriptionResult = await _mediator.Send(changeSubscriptionCommand); if (changeSubscriptionResult) { var changePlanCommand = new ChangePlan { AccountId = changeSubscriptionCommand.AccountId, Plan = changeSubscriptionCommand.Plan }; var changePlanResult = await _mediator.Send(changePlanCommand); if (changePlanResult) { return(Ok()); } } } return(BadRequest()); }
public static void CanChangePlan(ChangePlan command, AppPlan?plan, IAppPlansProvider appPlans) { Guard.NotNull(command); Validate.It(() => "Cannot change plan.", e => { if (string.IsNullOrWhiteSpace(command.PlanId)) { e(Not.Defined("Plan id"), nameof(command.PlanId)); return; } if (appPlans.GetPlan(command.PlanId) == null) { e("A plan with this id does not exist.", nameof(command.PlanId)); } if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor)) { e("Plan can only changed from the user who configured the plan initially."); } }); }
public static void CanChangePlan(ChangePlan command, AppPlan?plan, IAppPlansProvider appPlans) { Guard.NotNull(command, nameof(command)); Validate.It(e => { if (string.IsNullOrWhiteSpace(command.PlanId)) { e(Not.Defined(nameof(command.PlanId)), nameof(command.PlanId)); return; } if (appPlans.GetPlan(command.PlanId) == null) { e(T.Get("apps.plans.notFound"), nameof(command.PlanId)); } if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor)) { e(T.Get("apps.plans.notPlanOwner")); } }); }
protected Task On(ChangePlan command, CommandContext context) { return(handler.UpdateSyncedAsync <AppDomainObject>(context, async a => { GuardApp.CanChangePlan(command, a.Snapshot.Plan, appPlansProvider); if (command.FromCallback) { a.ChangePlan(command); } else { var result = await appPlansBillingManager.ChangePlanAsync(command.Actor.Identifier, a.Snapshot.Id, a.Snapshot.Name, command.PlanId); if (result is PlanChangedResult) { a.ChangePlan(command); } context.Complete(result); } })); }
public async Task ChangePlan_should_reset_plan_for_reset_plan() { var command = new ChangePlan { PlanId = planId }; A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, planId)) .Returns(new PlanResetResult()); await ExecuteCreateAsync(); var result = await sut.ExecuteAsync(CreateCommand(command)); Assert.True(result.Value is PlanResetResult); Assert.Null(sut.Snapshot.Plan); LastEvents .ShouldHaveSameEvents( CreateEvent(new AppPlanChanged { PlanId = null }) ); }
public async Task ChangePlan_should_create_events_and_update_state() { var command = new ChangePlan { PlanId = planIdPaid }; A.CallTo(() => appPlansBillingManager.ChangePlanAsync(Actor.Identifier, AppNamedId, planIdPaid)) .Returns(new PlanChangedResult()); await ExecuteCreateAsync(); var result = await sut.ExecuteAsync(CreateCommand(command)); Assert.True(result.Value is PlanChangedResult); Assert.Equal(planIdPaid, sut.Snapshot.Plan !.PlanId); LastEvents .ShouldHaveSameEvents( CreateEvent(new AppPlanChanged { PlanId = planIdPaid }) ); }
public async Task ChangePlan_from_callback_should_create_events_and_update_plan() { var command = new ChangePlan { PlanId = planIdPaid, FromCallback = true }; await ExecuteCreateAsync(); var result = await PublishIdempotentAsync(command); result.ShouldBeEquivalent(new EntitySavedResult(4)); Assert.Equal(planIdPaid, sut.Snapshot.Plan !.PlanId); LastEvents .ShouldHaveSameEvents( CreateEvent(new AppPlanChanged { PlanId = planIdPaid }) ); A.CallTo(() => appPlansBillingManager.ChangePlanAsync(A <string> ._, A <NamedId <DomainId> > ._, A <string?> ._, A <string?> ._)) .MustNotHaveHappened(); }
public void PlanToUnapplyShouldLoadCorrectMigrationsAndUnapplyThemAgainstTheDatabase() { var testSubject = new ChangePlan(Do.Unapply, new[] {4, 3}); testSubject.ApplyTo(this._database, this._definedMigrations.ToLoaders()); this._database.UnappliedMigrations.Should().ContainInOrder(MigrationsForVersions(this._definedMigrations, 4, 3)); }
public void PlanToUnapplyShouldSetDatabaseVersionToOneLessThanLastMigrationUnapplied() { var testSubject = new ChangePlan(Do.Unapply, new[] {4, 3}); testSubject.ApplyTo(this._database, this._definedMigrations.ToLoaders()); this._database.CurrentVersion.Result.Should().Be(2); }
public void ShouldCommitAllChangesToTheDatabase() { var testSubject = new ChangePlan(Do.Unapply, new[] {4, 3}); testSubject.ApplyTo(this._database, this._definedMigrations.ToLoaders()); this._database.CommittedTheChanges.Should().BeTrue(); }
public void ResetPlan(ChangePlan command) { RaiseEvent(SimpleMapper.Map(command, new AppPlanReset())); }
public void ChangePlan(ChangePlan command) { RaiseEvent(SimpleMapper.Map(command, new AppPlanChanged())); }
public MigrationSet(ChangePlan plan, IDatabase database, params IMigrationLoader[] loaders) { this.Plan = plan; this.Loaders = loaders; this._database = database; }