-
Notifications
You must be signed in to change notification settings - Fork 3
user registration: handle accepted policies #1187
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
base: main
Are you sure you want to change the base?
Changes from all commits
1c980d4
8d9a9ca
0d15403
9893bc0
8b205cf
2fcfbfe
9e2586b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |||||||||||
| namespace App\Jobs; | ||||||||||||
|
|
||||||||||||
| use App\User; | ||||||||||||
| use App\UserTermsOfUseAcceptance; | ||||||||||||
| use App\Policy; | ||||||||||||
| use App\PolicyAcceptance; | ||||||||||||
| use App\TermsOfUseVersion | ||||||||||||
| use Illuminate\Support\Facades\Hash; | ||||||||||||
|
|
||||||||||||
| class UserCreateJob extends Job { | ||||||||||||
|
|
@@ -12,11 +16,12 @@ class UserCreateJob extends Job { | |||||||||||
|
|
||||||||||||
| private $verified; | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $this->acceptedPolicyIds assigned in constructor with no property declaration
Suggested change
|
||||||||||||
|
|
||||||||||||
| public function __construct($email, $password, $verified = false) { | ||||||||||||
| public function __construct($email, $password, $acceptedPolicyIds, $verified = false) { | ||||||||||||
| // TODO maybe pass in an unsaved eloquent model? | ||||||||||||
| // // but that would make CLI job creation hard | ||||||||||||
| $this->email = $email; | ||||||||||||
| $this->password = $password; | ||||||||||||
| $this->acceptedPolicyIds = $acceptedPolicyIds; | ||||||||||||
| $this->verified = false; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -30,6 +35,34 @@ public function handle() { | |||||||||||
| 'verified' => $this->verified, | ||||||||||||
| ]); | ||||||||||||
|
|
||||||||||||
| // accept latest Terms of Use Policy automatically | ||||||||||||
| $latestToU = TermsOfUseVersion::latestActiveVersion(); | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. references to classes that have been deleted since T430532 |
||||||||||||
| if ($latestToU) { | ||||||||||||
| UserTermsOfUseAcceptance::create([ | ||||||||||||
| 'user_id' => $user->id, | ||||||||||||
| 'tou_version' => $latestToU->version, | ||||||||||||
| 'tou_accepted_at' => now(), | ||||||||||||
| ]); | ||||||||||||
| } else { | ||||||||||||
| Log::warning("No active Terms of Use version found when creating user {$user->email} (ID {$user->id})."); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (is_array($this->acceptedPolicyIds)) { | ||||||||||||
| foreach($this->acceptedPolicyIds as $acceptedPolicyId) { | ||||||||||||
| $policy = Policy::find($acceptedPolicyId); | ||||||||||||
|
|
||||||||||||
| if ($policy) { | ||||||||||||
| PolicyAcceptance::create([ | ||||||||||||
| 'user_id' => $user->id, | ||||||||||||
| 'policy_id' => $policy->id, | ||||||||||||
| 'accepted_at' => now(), | ||||||||||||
| ]); | ||||||||||||
| } else { | ||||||||||||
| Log::warning("Policy ID '{$acceptedPolicyId}' not found when creating user {$user->email} (ID {$user->id})."); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return $user; | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Jobs; | ||
|
|
||
| use App\Policy; | ||
| use App\Jobs\CreateFirstTermsOfUseVersionJob; | ||
| use App\Jobs\UserCreateJob; | ||
| use App\TermsOfUseVersion; | ||
| use App\PolicyAcceptance; | ||
| use App\UserTermsOfUseAcceptance; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Tests\TestCase; | ||
| use Carbon\CarbonImmutable; | ||
|
|
||
| class UserAcceptedPoliciesTest extends TestCase { | ||
| use RefreshDatabase; | ||
|
|
||
| public function testUserRegistrationAcceptsNone(): void { | ||
| $email = 'test+' . uniqid('', true) . '@example.com'; | ||
| $user = (new UserCreateJob($email, 'thisisapassword123', null, true))->handle(); | ||
|
|
||
| $this->assertEquals( | ||
| PolicyAcceptance::all()->count(), | ||
| 0 | ||
| ); | ||
| } | ||
|
|
||
| public function testUserRegistrationAcceptsOne(): void { | ||
| $termsOfUse = new Policy; | ||
| $termsOfUse->policy_type = 'terms-of-use'; | ||
| $termsOfUse->active_from = CarbonImmutable::now(); | ||
| $termsOfUse->content_vue_file = 'termsOfUse.vue'; | ||
| $termsOfUse->save(); | ||
|
|
||
| $email = 'test+' . uniqid('', true) . '@example.com'; | ||
| $user = (new UserCreateJob($email, 'thisisapassword123', [$termsOfUse->id], true))->handle(); | ||
|
|
||
| $this->assertDatabaseHas('policy_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $termsOfUse->id, | ||
| ]); | ||
| } | ||
|
|
||
| public function testUserRegistrationAcceptsTwo(): void { | ||
| $termsOfUse = new Policy; | ||
| $termsOfUse->policy_type = 'terms-of-use'; | ||
| $termsOfUse->active_from = CarbonImmutable::now(); | ||
| $termsOfUse->content_vue_file = 'termsOfUse.vue'; | ||
| $termsOfUse->save(); | ||
|
|
||
| $hostingPolicy = new Policy; | ||
| $hostingPolicy->policy_type = 'hosting-policy'; | ||
| $hostingPolicy->active_from = CarbonImmutable::now(); | ||
| $hostingPolicy->content_vue_file = 'hostingPolicy.vue'; | ||
| $hostingPolicy->save(); | ||
|
|
||
| $email = 'test+' . uniqid('', true) . '@example.com'; | ||
| $user = (new UserCreateJob($email, 'thisisapassword123', [$termsOfUse->id, $hostingPolicy->id], true))->handle(); | ||
|
|
||
| $this->assertDatabaseHas('policy_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $termsOfUse->id, | ||
| ]); | ||
|
|
||
| $this->assertDatabaseHas('policy_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $hostingPolicy->id, | ||
| ]); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Jobs; | ||
|
|
||
| use App\Jobs\CreateFirstTermsOfUseVersionJob; | ||
| use App\Jobs\UserCreateJob; | ||
| use App\TermsOfUseVersion; | ||
| use App\UserTermsOfUseAcceptance; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Tests\TestCase; | ||
|
|
||
| class UserTermsOfUseAcceptanceTest extends TestCase { | ||
| use RefreshDatabase; | ||
|
|
||
| public function testUserCreationCreatesTouAcceptance(): void { | ||
| (new CreateFirstTermsOfUseVersionJob())->handle(); | ||
| $email = 'test+' . uniqid('', true) . '@example.com'; | ||
| $user = (new UserCreateJob($email, 'thisisapassword123', null, true))->handle(); | ||
|
|
||
| $this->assertDatabaseHas('tou_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'tou_version' => TermsOfUseVersion::latestActiveVersion()->version, | ||
| ]); | ||
|
|
||
| $rows = UserTermsOfUseAcceptance::where('user_id', $user->id)->get(); | ||
| $this->assertCount(1, $rows); | ||
| $acceptance = $rows->first(); | ||
|
|
||
| $this->assertSame(TermsOfUseVersion::latestActiveVersion()->version, $acceptance->tou_version); | ||
| $this->assertNotNull($acceptance->tou_accepted_at); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missng a
;