-
Notifications
You must be signed in to change notification settings - Fork 0
fix: send trial emails synchronously so sent flag reflects delivery #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dan2k3k4
wants to merge
2
commits into
dev
Choose a base branch
from
advisor/011-trial-email-sent-flag
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Jobs\Trial; | ||
|
|
||
| use App\Enums\UserGroupRoleEnum; | ||
| use App\Events\PolydockAppInstanceCreatedWithNewStatus; | ||
| use App\Events\PolydockAppInstanceStatusChanged; | ||
| use App\Jobs\ProcessPolydockAppInstanceJobs\Trial\ProcessMidtrialEmailJob; | ||
| use App\Mail\AppInstanceMidtrialMail; | ||
| use App\Models\PolydockAppInstance; | ||
| use App\Models\PolydockStore; | ||
| use App\Models\PolydockStoreApp; | ||
| use App\Models\User; | ||
| use App\Models\UserGroup; | ||
| use App\Polydock\Apps\Generic\PolydockAiApp; | ||
| use App\Polydock\Core\Enums\PolydockAppInstanceStatus; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Illuminate\Support\Facades\Mail; | ||
| use Mockery; | ||
| use RuntimeException; | ||
| use Tests\TestCase; | ||
|
|
||
| class ProcessMidtrialEmailJobTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| private PolydockAppInstance $instance; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Prevent the NEW-status save below from dispatching the real | ||
| // lifecycle jobs (which would reach out to Lagoon). | ||
| Event::fake([ | ||
| PolydockAppInstanceCreatedWithNewStatus::class, | ||
| PolydockAppInstanceStatusChanged::class, | ||
| ]); | ||
|
|
||
| $store = PolydockStore::factory()->create(); | ||
| $storeApp = PolydockStoreApp::factory()->create([ | ||
| 'polydock_store_id' => $store->id, | ||
| 'send_midtrial_email' => true, | ||
| ]); | ||
|
|
||
| $group = UserGroup::factory()->create(); | ||
| $owner = User::factory()->create(['email' => 'owner@example.com']); | ||
| $group->users()->attach($owner->id, ['role' => UserGroupRoleEnum::OWNER->value]); | ||
|
|
||
| $this->instance = new PolydockAppInstance; | ||
| $this->instance->polydock_store_app_id = $storeApp->id; | ||
| $this->instance->user_group_id = $group->id; | ||
| $this->instance->name = 'midtrial-test'; | ||
| $this->instance->app_type = PolydockAiApp::class; | ||
| $this->instance->status = PolydockAppInstanceStatus::NEW; | ||
| $this->instance->is_trial = true; | ||
| // Reminder is due (past) but the trial itself has not expired (future). | ||
| $this->instance->send_midtrial_email_at = now()->subHour(); | ||
| $this->instance->trial_ends_at = now()->addDays(3); | ||
| $this->instance->midtrial_email_sent = false; | ||
| $this->instance->save(); | ||
| } | ||
|
|
||
| public function test_midtrial_email_is_sent_and_flag_is_set(): void | ||
| { | ||
| Mail::fake(); | ||
|
|
||
| (new ProcessMidtrialEmailJob($this->instance->id))->handle(); | ||
|
|
||
| Mail::assertSent(AppInstanceMidtrialMail::class); | ||
|
|
||
| $this->assertTrue($this->instance->fresh()->midtrial_email_sent); | ||
| } | ||
|
|
||
| public function test_flag_stays_false_when_send_throws(): void | ||
| { | ||
| // Force the synchronous send to fail so the job should throw before | ||
| // the sent flag is updated. | ||
| $pendingMail = Mockery::mock(); | ||
| $pendingMail->shouldReceive('cc')->andReturnSelf(); | ||
| $pendingMail->shouldReceive('send') | ||
| ->andThrow(new RuntimeException('SMTP failure')); | ||
|
|
||
| Mail::shouldReceive('to')->andReturn($pendingMail); | ||
|
|
||
| try { | ||
| (new ProcessMidtrialEmailJob($this->instance->id))->handle(); | ||
| $this->fail('Expected the job to propagate the send exception.'); | ||
| } catch (RuntimeException $e) { | ||
| $this->assertSame('SMTP failure', $e->getMessage()); | ||
| } | ||
|
|
||
| $this->assertFalse( | ||
| $this->instance->fresh()->midtrial_email_sent, | ||
| 'The sent flag must remain false when delivery fails so the job can retry.' | ||
| ); | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
tests/Feature/Jobs/Trial/ProcessOneDayLeftEmailJobTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Jobs\Trial; | ||
|
|
||
| use App\Enums\UserGroupRoleEnum; | ||
| use App\Events\PolydockAppInstanceCreatedWithNewStatus; | ||
| use App\Events\PolydockAppInstanceStatusChanged; | ||
| use App\Jobs\ProcessPolydockAppInstanceJobs\Trial\ProcessOneDayLeftEmailJob; | ||
| use App\Mail\AppInstanceOneDayLeftMail; | ||
| use App\Models\PolydockAppInstance; | ||
| use App\Models\PolydockStore; | ||
| use App\Models\PolydockStoreApp; | ||
| use App\Models\User; | ||
| use App\Models\UserGroup; | ||
| use App\Polydock\Apps\Generic\PolydockAiApp; | ||
| use App\Polydock\Core\Enums\PolydockAppInstanceStatus; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Illuminate\Support\Facades\Mail; | ||
| use Mockery; | ||
| use RuntimeException; | ||
| use Tests\TestCase; | ||
|
|
||
| class ProcessOneDayLeftEmailJobTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| private PolydockAppInstance $instance; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Prevent the NEW-status save below from dispatching the real | ||
| // lifecycle jobs (which would reach out to Lagoon). | ||
| Event::fake([ | ||
| PolydockAppInstanceCreatedWithNewStatus::class, | ||
| PolydockAppInstanceStatusChanged::class, | ||
| ]); | ||
|
|
||
| $store = PolydockStore::factory()->create(); | ||
| $storeApp = PolydockStoreApp::factory()->create([ | ||
| 'polydock_store_id' => $store->id, | ||
| 'send_one_day_left_email' => true, | ||
| ]); | ||
|
|
||
| $group = UserGroup::factory()->create(); | ||
| $owner = User::factory()->create(['email' => 'owner@example.com']); | ||
| $group->users()->attach($owner->id, ['role' => UserGroupRoleEnum::OWNER->value]); | ||
|
|
||
| $this->instance = new PolydockAppInstance; | ||
| $this->instance->polydock_store_app_id = $storeApp->id; | ||
| $this->instance->user_group_id = $group->id; | ||
| $this->instance->name = 'one-day-left-test'; | ||
| $this->instance->app_type = PolydockAiApp::class; | ||
| $this->instance->status = PolydockAppInstanceStatus::NEW; | ||
| $this->instance->is_trial = true; | ||
| // Reminder is due (past) but the trial itself has not expired (future). | ||
| $this->instance->send_one_day_left_email_at = now()->subHour(); | ||
| $this->instance->trial_ends_at = now()->addDay(); | ||
| $this->instance->one_day_left_email_sent = false; | ||
| $this->instance->save(); | ||
| } | ||
|
|
||
| public function test_one_day_left_email_is_sent_and_flag_is_set(): void | ||
| { | ||
| Mail::fake(); | ||
|
|
||
| (new ProcessOneDayLeftEmailJob($this->instance->id))->handle(); | ||
|
|
||
| Mail::assertSent(AppInstanceOneDayLeftMail::class); | ||
|
|
||
| $this->assertTrue($this->instance->fresh()->one_day_left_email_sent); | ||
| } | ||
|
|
||
| public function test_flag_stays_false_when_send_throws(): void | ||
| { | ||
| // Force the synchronous send to fail so the job should throw before | ||
| // the sent flag is updated. | ||
| $pendingMail = Mockery::mock(); | ||
| $pendingMail->shouldReceive('cc')->andReturnSelf(); | ||
| $pendingMail->shouldReceive('send') | ||
| ->andThrow(new RuntimeException('SMTP failure')); | ||
|
|
||
| Mail::shouldReceive('to')->andReturn($pendingMail); | ||
|
|
||
| try { | ||
| (new ProcessOneDayLeftEmailJob($this->instance->id))->handle(); | ||
| $this->fail('Expected the job to propagate the send exception.'); | ||
| } catch (RuntimeException $e) { | ||
| $this->assertSame('SMTP failure', $e->getMessage()); | ||
| } | ||
|
|
||
| $this->assertFalse( | ||
| $this->instance->fresh()->one_day_left_email_sent, | ||
| 'The sent flag must remain false when delivery fails so the job can retry.' | ||
| ); | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
tests/Feature/Jobs/Trial/ProcessTrialCompleteEmailJobTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Jobs\Trial; | ||
|
|
||
| use App\Enums\UserGroupRoleEnum; | ||
| use App\Events\PolydockAppInstanceCreatedWithNewStatus; | ||
| use App\Events\PolydockAppInstanceStatusChanged; | ||
| use App\Jobs\ProcessPolydockAppInstanceJobs\Trial\ProcessTrialCompleteEmailJob; | ||
| use App\Mail\AppInstanceTrialCompleteMail; | ||
| use App\Models\PolydockAppInstance; | ||
| use App\Models\PolydockStore; | ||
| use App\Models\PolydockStoreApp; | ||
| use App\Models\User; | ||
| use App\Models\UserGroup; | ||
| use App\Polydock\Apps\Generic\PolydockAiApp; | ||
| use App\Polydock\Core\Enums\PolydockAppInstanceStatus; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Illuminate\Support\Facades\Mail; | ||
| use Mockery; | ||
| use RuntimeException; | ||
| use Tests\TestCase; | ||
|
|
||
| class ProcessTrialCompleteEmailJobTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| private PolydockAppInstance $instance; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Prevent the NEW-status save below from dispatching the real | ||
| // lifecycle jobs (which would reach out to Lagoon). | ||
| Event::fake([ | ||
| PolydockAppInstanceCreatedWithNewStatus::class, | ||
| PolydockAppInstanceStatusChanged::class, | ||
| ]); | ||
|
|
||
| $store = PolydockStore::factory()->create(); | ||
| $storeApp = PolydockStoreApp::factory()->create([ | ||
| 'polydock_store_id' => $store->id, | ||
| 'send_trial_complete_email' => true, | ||
| ]); | ||
|
|
||
| $group = UserGroup::factory()->create(); | ||
| $owner = User::factory()->create(['email' => 'owner@example.com']); | ||
| $group->users()->attach($owner->id, ['role' => UserGroupRoleEnum::OWNER->value]); | ||
|
|
||
| $this->instance = new PolydockAppInstance; | ||
| $this->instance->polydock_store_app_id = $storeApp->id; | ||
| $this->instance->user_group_id = $group->id; | ||
| $this->instance->name = 'trial-complete-test'; | ||
| $this->instance->app_type = PolydockAiApp::class; | ||
| $this->instance->status = PolydockAppInstanceStatus::NEW; | ||
| $this->instance->is_trial = true; | ||
| $this->instance->trial_ends_at = now()->subDay(); | ||
| $this->instance->trial_complete_email_sent = false; | ||
| $this->instance->save(); | ||
| } | ||
|
|
||
| public function test_trial_complete_email_is_sent_and_flag_is_set(): void | ||
| { | ||
| Mail::fake(); | ||
|
|
||
| (new ProcessTrialCompleteEmailJob($this->instance->id))->handle(); | ||
|
|
||
| Mail::assertSent(AppInstanceTrialCompleteMail::class); | ||
|
|
||
| $this->assertTrue($this->instance->fresh()->trial_complete_email_sent); | ||
| } | ||
|
|
||
| public function test_flag_stays_false_when_send_throws(): void | ||
| { | ||
| // Force the synchronous send to fail so the job should throw before | ||
| // the sent flag is updated. | ||
| $pendingMail = Mockery::mock(); | ||
| $pendingMail->shouldReceive('cc')->andReturnSelf(); | ||
| $pendingMail->shouldReceive('send') | ||
| ->andThrow(new RuntimeException('SMTP failure')); | ||
|
|
||
| Mail::shouldReceive('to')->andReturn($pendingMail); | ||
|
|
||
| try { | ||
| (new ProcessTrialCompleteEmailJob($this->instance->id))->handle(); | ||
| $this->fail('Expected the job to propagate the send exception.'); | ||
| } catch (RuntimeException $e) { | ||
| $this->assertSame('SMTP failure', $e->getMessage()); | ||
| } | ||
|
|
||
| $this->assertFalse( | ||
| $this->instance->fresh()->trial_complete_email_sent, | ||
| 'The sent flag must remain false when delivery fails so the job can retry.' | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.