-
Notifications
You must be signed in to change notification settings - Fork 3
Add endpoint for creating a policy acceptance for a specific user #1197
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3eb1839
Add endpoint for creating a policy acceptance for a specific user
dati18 99b43ca
add PolicyAcceptanceController class
dati18 eb10076
add test cases
dati18 6aae19d
Add tests to check original accepted_at of accepted policy
dati18 4491636
Add non-integer policy id test case
dati18 80bc9ee
put endpoint under v1/policy_acceptances instead of root
dati18 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Policy; | ||
| use App\PolicyAcceptance; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
|
|
||
| class PolicyAcceptanceController extends Controller { | ||
| public function store(Request $request): JsonResponse { | ||
| $request->validate([ | ||
| 'policy_ids' => ['required', 'array'], | ||
| 'policy_ids.*' => ['integer'], | ||
| ]); | ||
|
|
||
| $policyIds = $request->input('policy_ids'); | ||
|
|
||
| // Check all policy IDs exist before writing anything | ||
| $existingIds = Policy::whereIn('id', $policyIds)->pluck('id')->all(); | ||
| $missingIds = array_values(array_diff($policyIds, $existingIds)); | ||
|
|
||
| if (!empty($missingIds)) { | ||
| return response()->json([ | ||
| 'success' => false, | ||
| 'message' => 'Some policy IDs do not exist.', | ||
| 'data' => ['missing_policy_ids' => $missingIds], | ||
| ], 400); | ||
| } | ||
|
|
||
| $userId = $request->user()->id; | ||
|
|
||
| foreach ($policyIds as $policyId) { | ||
| // Ignore if the user has already accepted this policy | ||
| PolicyAcceptance::firstOrCreate( | ||
| ['user_id' => $userId, 'policy_id' => $policyId], | ||
| ['accepted_at' => now()], | ||
|
tarrow marked this conversation as resolved.
|
||
| ); | ||
| } | ||
|
|
||
| return response()->json(['success' => true]); | ||
| } | ||
| } | ||
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,157 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Routes; | ||
|
|
||
| use App\Policy; | ||
| use App\PolicyAcceptance; | ||
| use App\User; | ||
| use Carbon\Carbon; | ||
| use Carbon\CarbonImmutable; | ||
| use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
| use Tests\TestCase; | ||
|
|
||
| class PolicyAcceptanceControllerTest extends TestCase { | ||
| protected $route = 'v1/policy_acceptances'; | ||
|
|
||
| use DatabaseTransactions; | ||
|
|
||
| private function makePolicy(string $type = 'terms-of-use'): Policy { | ||
| $policy = new Policy(); | ||
| $policy->policy_type = $type; | ||
| $policy->active_from = CarbonImmutable::now(); | ||
| $policy->content_vue_file = $type . '/version-1.vue'; | ||
| $policy->save(); | ||
|
|
||
| return $policy; | ||
| } | ||
|
|
||
| public function testUnauthenticatedRequestResponds401(): void { | ||
| $this->json('PUT', $this->route) | ||
| ->assertStatus(401); | ||
| } | ||
|
|
||
| public function testAcceptSinglePolicy(): void { | ||
| $user = User::factory()->create(); | ||
| $policy = $this->makePolicy(); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
|
tarrow marked this conversation as resolved.
|
||
| ->json('PUT', $this->route, ['policy_ids' => [$policy->id]]) | ||
| ->assertStatus(200) | ||
| ->assertJson(['success' => true]); | ||
|
|
||
| $this->assertDatabaseHas('policy_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| ]); | ||
| } | ||
|
|
||
| public function testAcceptMultiplePolicies(): void { | ||
| $user = User::factory()->create(); | ||
| $termsOfUse = $this->makePolicy('terms-of-use'); | ||
| $hostingPolicy = $this->makePolicy('hosting-policy'); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, ['policy_ids' => [$termsOfUse->id, $hostingPolicy->id]]) | ||
| ->assertStatus(200) | ||
| ->assertJson(['success' => true]); | ||
|
|
||
| $this->assertDatabaseHas('policy_acceptances', ['user_id' => $user->id, 'policy_id' => $termsOfUse->id]); | ||
| $this->assertDatabaseHas('policy_acceptances', ['user_id' => $user->id, 'policy_id' => $hostingPolicy->id]); | ||
| } | ||
|
|
||
| public function testAlreadyAcceptedPolicyIsIgnored(): void { | ||
| $user = User::factory()->create(); | ||
| $policy = $this->makePolicy(); | ||
|
|
||
| PolicyAcceptance::create([ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| 'accepted_at' => now(), | ||
| ]); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, ['policy_ids' => [$policy->id]]) | ||
| ->assertStatus(200) | ||
| ->assertJson(['success' => true]); | ||
|
|
||
| $this->assertSame(1, PolicyAcceptance::where([ | ||
|
tarrow marked this conversation as resolved.
|
||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| ])->count()); | ||
| } | ||
|
|
||
| public function testAlreadyAcceptedPolicyKeepsOriginalAcceptedAt(): void { | ||
| $user = User::factory()->create(); | ||
| $policy = $this->makePolicy(); | ||
|
|
||
| $originalAcceptedAt = CarbonImmutable::create(2026, 7, 1, 10, 0, 0); | ||
| Carbon::setTestNow(CarbonImmutable::create(2026, 7, 2, 10, 0, 0)); | ||
|
|
||
| PolicyAcceptance::create([ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| 'accepted_at' => $originalAcceptedAt, | ||
| ]); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, ['policy_ids' => [$policy->id]]) | ||
| ->assertStatus(200) | ||
| ->assertJson(['success' => true]); | ||
|
|
||
| $this->assertEquals($originalAcceptedAt->toDateTimeString(), PolicyAcceptance::where([ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| ])->first()->accepted_at->toDateTimeString()); | ||
| } | ||
|
|
||
| public function testNonExistentPolicyIdReturns400(): void { | ||
| $user = User::factory()->create(); | ||
| $policy = $this->makePolicy(); | ||
| $nonExistentId = 999999; | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, ['policy_ids' => [$policy->id, $nonExistentId]]) | ||
| ->assertStatus(400) | ||
| ->assertJsonFragment(['success' => false]) | ||
| ->assertJsonFragment(['missing_policy_ids' => [$nonExistentId]]); | ||
|
|
||
| // Nothing should have been written | ||
| $this->assertDatabaseMissing('policy_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| ]); | ||
| } | ||
|
|
||
| public function testMissingPolicyIdsFieldReturns422(): void { | ||
|
tarrow marked this conversation as resolved.
|
||
| $user = User::factory()->create(); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, []) | ||
| ->assertStatus(422) | ||
| ->assertJsonStructure(['errors' => ['policy_ids']]); | ||
| } | ||
|
|
||
| public function testPolicyIdsNotAnArrayReturns422(): void { | ||
| $user = User::factory()->create(); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, ['policy_ids' => 'not-an-array']) | ||
| ->assertStatus(422) | ||
| ->assertJsonStructure(['errors' => ['policy_ids']]); | ||
| } | ||
|
tarrow marked this conversation as resolved.
|
||
|
|
||
| public function testPolicyIdsContainingNonIntegerReturns422(): void { | ||
| $user = User::factory()->create(); | ||
| $policy = $this->makePolicy(); | ||
|
|
||
| $this->actingAs($user, 'api') | ||
| ->json('PUT', $this->route, ['policy_ids' => [$policy->id, 'abc']]) | ||
| ->assertStatus(422) | ||
| ->assertJsonStructure(['errors' => ['policy_ids.1']]); | ||
|
|
||
| $this->assertDatabaseMissing('policy_acceptances', [ | ||
| 'user_id' => $user->id, | ||
| 'policy_id' => $policy->id, | ||
| ]); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.