1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Company;
use App\Models\Connection;
use App\Models\Student;
use App\Policies\ConnectionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ConnectionApiTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_connections()
{
Connection::factory()->count(2)->create();
$admin = Admin::factory()->create();
Sanctum::actingAs($admin, ['admin']);
$response = $this->getJson('/api/connections');
$response->assertStatus(200)
->assertJsonStructure([
'data' => [
['id', 'student_id', 'company_id', 'status', 'skill_match_percentage']
]
]);
}
public function test_store_creates_connection()
{
$student = Student::factory()->create();
$company = Company::factory()->create();
$data = [
'student_id' => $student->id,
'company_id' => $company->id,
'status' => true,
'skill_match_percentage' => ConnectionService::calculateSkillMatchPercentage($student->id, $company->id)
];
Sanctum::actingAs($student, ['student']);
$response = $this->postJson('/api/connections', $data);
$response->assertStatus(201)
->assertJsonStructure(['data' => ['id']]);
}
public function test_show_returns_connection()
{
$student = Student::factory()->create();
$connection = Connection::factory()->create(['student_id' => $student->id]);
Sanctum::actingAs($student, ['student']);
$response = $this->getJson("/api/connections/{$connection->id}");
$response->assertStatus(200)
->assertJson([
'data' => [
'id' => $connection->id,
'student_id' => $connection->student_id,
'company_id' => $connection->company_id,
'status' => $connection->status,
'skill_match_percentage' => $connection->skill_match_percentage,
]
]);
}
public function test_update_modifies_connection()
{
$connection = Connection::factory()->create();
$data = [
'student_id' => $connection->student_id,
'company_id' => $connection->company_id,
'status' => false,
];
$student = Student::find($connection->student_id);
Sanctum::actingAs($student, ['student']);
$response = $this->patchJson("/api/connections/{$connection->id}", $data);
$response->assertStatus(200)
->assertJsonStructure(['data' => ['id', 'student_id', 'company_id', 'status']]);
}
public function test_destroy_deletes_connection()
{
$connection = Connection::factory()->create();
$student = Student::find($connection->student_id);
Sanctum::actingAs($student, ['student']);
$response = $this->deleteJson("/api/connections/{$connection->id}");
Sanctum::actingAs($student, ['student']);
$response->assertStatus(200);
$this->assertDatabaseMissing('connections', ['id' => $connection->id]);
}
public function test_student_can_view_own_connections()
{
$student = Student::factory()->create();
$otherStudent = Student::factory()->create();
$company = Company::factory()->create();
// 2 connections for current student, 1 for someone else
Connection::factory()->count(2)->create(['student_id' => $student->id, 'company_id' => $company->id]);
Connection::factory()->create(['student_id' => $otherStudent->id, 'company_id' => $company->id]);
Sanctum::actingAs($student, ['student']);
$response = $this->getJson("/api/connections/student/{$student->id}");
$response->assertStatus(200)
->assertJsonCount(2, 'data');
}
public function test_student_cannot_view_connections_of_other_student()
{
$student = Student::factory()->create();
$otherStudent = Student::factory()->create();
$company = Company::factory()->create();
Connection::factory()->create(['student_id' => $otherStudent->id, 'company_id' => $company->id]);
Sanctum::actingAs($student, ['student']);
$response = $this->getJson("/api/connections/student/{$otherStudent->id}");
$response->assertStatus(403);
}
public function test_company_can_view_own_connections()
{
$student = Student::factory()->create();
$company = Company::factory()->create();
$otherCompany = Company::factory()->create();
Connection::factory()->count(2)->create(['student_id' => $student->id, 'company_id' => $company->id]);
Connection::factory()->create(['student_id' => $student->id, 'company_id' => $otherCompany->id]);
Sanctum::actingAs($company, ['company']);
$response = $this->getJson("/api/connections/company/{$company->id}");
$response->assertStatus(200)
->assertJsonCount(2, 'data');
}
public function test_company_cannot_view_connections_of_other_company()
{
$student = Student::factory()->create();
$company = Company::factory()->create();
$otherCompany = Company::factory()->create();
Connection::factory()->create(['student_id' => $student->id, 'company_id' => $otherCompany->id]);
Sanctum::actingAs($company, ['company']);
$response = $this->getJson("/api/connections/company/{$otherCompany->id}");
$response->assertStatus(403);
}
public function test_admin_can_view_connections_of_any_student_or_company()
{
$student = Student::factory()->create();
$company = Company::factory()->create();
$admin = Admin::factory()->create();
Connection::factory()->count(2)->create(['student_id' => $student->id, 'company_id' => $company->id]);
Sanctum::actingAs($admin, ['admin']);
$resStudent = $this->getJson("/api/connections/student/{$student->id}");
$resCompany = $this->getJson("/api/connections/company/{$company->id}");
$resStudent->assertStatus(200)->assertJsonCount(2, 'data');
$resCompany->assertStatus(200)->assertJsonCount(2, 'data');
}
}
|