Skip to content

Commit 0ca4ecc

Browse files
committed
add test cases
1 parent a721c92 commit 0ca4ecc

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Tests\Routes;
4+
5+
use App\Policy;
6+
use App\PolicyAcceptance;
7+
use App\User;
8+
use Carbon\CarbonImmutable;
9+
use Illuminate\Foundation\Testing\DatabaseTransactions;
10+
use Tests\TestCase;
11+
12+
class PolicyAcceptanceControllerTest extends TestCase {
13+
protected $route = 'policy_acceptances';
14+
15+
use DatabaseTransactions;
16+
17+
private function makePolicy(string $type = 'terms-of-use'): Policy {
18+
$policy = new Policy();
19+
$policy->policy_type = $type;
20+
$policy->active_from = CarbonImmutable::now();
21+
$policy->content_vue_file = $type . '/version-1.vue';
22+
$policy->save();
23+
24+
return $policy;
25+
}
26+
27+
public function testUnauthenticatedRequestResponds401(): void {
28+
$this->json('PUT', $this->route)
29+
->assertStatus(401);
30+
}
31+
32+
public function testAcceptSinglePolicy(): void {
33+
$user = User::factory()->create();
34+
$policy = $this->makePolicy();
35+
36+
$this->actingAs($user, 'api')
37+
->json('PUT', $this->route, ['policy_ids' => [$policy->id]])
38+
->assertStatus(200)
39+
->assertJson(['success' => true]);
40+
41+
$this->assertDatabaseHas('policy_acceptances', [
42+
'user_id' => $user->id,
43+
'policy_id' => $policy->id,
44+
]);
45+
}
46+
47+
public function testAcceptMultiplePolicies(): void {
48+
$user = User::factory()->create();
49+
$termsOfUse = $this->makePolicy('terms-of-use');
50+
$hostingPolicy = $this->makePolicy('hosting-policy');
51+
52+
$this->actingAs($user, 'api')
53+
->json('PUT', $this->route, ['policy_ids' => [$termsOfUse->id, $hostingPolicy->id]])
54+
->assertStatus(200)
55+
->assertJson(['success' => true]);
56+
57+
$this->assertDatabaseHas('policy_acceptances', ['user_id' => $user->id, 'policy_id' => $termsOfUse->id]);
58+
$this->assertDatabaseHas('policy_acceptances', ['user_id' => $user->id, 'policy_id' => $hostingPolicy->id]);
59+
}
60+
61+
public function testAlreadyAcceptedPolicyIsIgnored(): void {
62+
$user = User::factory()->create();
63+
$policy = $this->makePolicy();
64+
65+
PolicyAcceptance::create([
66+
'user_id' => $user->id,
67+
'policy_id' => $policy->id,
68+
'accepted_at' => now(),
69+
]);
70+
71+
$this->actingAs($user, 'api')
72+
->json('PUT', $this->route, ['policy_ids' => [$policy->id]])
73+
->assertStatus(200)
74+
->assertJson(['success' => true]);
75+
76+
$this->assertSame(1, PolicyAcceptance::where([
77+
'user_id' => $user->id,
78+
'policy_id' => $policy->id,
79+
])->count());
80+
}
81+
82+
public function testNonExistentPolicyIdReturns400(): void {
83+
$user = User::factory()->create();
84+
$policy = $this->makePolicy();
85+
$nonExistentId = 999999;
86+
87+
$this->actingAs($user, 'api')
88+
->json('PUT', $this->route, ['policy_ids' => [$policy->id, $nonExistentId]])
89+
->assertStatus(400)
90+
->assertJsonFragment(['success' => false])
91+
->assertJsonFragment(['missing_policy_ids' => [$nonExistentId]]);
92+
93+
// Nothing should have been written
94+
$this->assertDatabaseMissing('policy_acceptances', [
95+
'user_id' => $user->id,
96+
'policy_id' => $policy->id,
97+
]);
98+
}
99+
100+
public function testMissingPolicyIdsFieldReturns422(): void {
101+
$user = User::factory()->create();
102+
103+
$this->actingAs($user, 'api')
104+
->json('PUT', $this->route, [])
105+
->assertStatus(422)
106+
->assertJsonStructure(['errors' => ['policy_ids']]);
107+
}
108+
109+
public function testPolicyIdsNotAnArrayReturns422(): void {
110+
$user = User::factory()->create();
111+
112+
$this->actingAs($user, 'api')
113+
->json('PUT', $this->route, ['policy_ids' => 'not-an-array'])
114+
->assertStatus(422)
115+
->assertJsonStructure(['errors' => ['policy_ids']]);
116+
}
117+
}

0 commit comments

Comments
 (0)