Skip to content

Commit 56ccf36

Browse files
author
Jovert Lota Palonpon
committed
[Testing] Added more authentication tests
1 parent a5687e2 commit 56ccf36

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

app/Http/Controllers/Api/Auth/SessionsController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public function refresh()
151151
*/
152152
public function signout() : JsonResponse
153153
{
154+
$user = $this->guard()->user();
155+
156+
$user->auth_token = null;
157+
$user->update();
158+
154159
$this->guard()->logout();
155160

156161
return response()->json(['message' => 'Successfully signed out']);

tests/Feature/Api/Auth/SessionsTest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public function a_user_can_be_identified()
1212
{
1313
$user = User::first();
1414

15-
$attributes = [
15+
$payload = [
1616
'username' => $user->username,
1717
];
1818

19-
$this->post(route('api.auth.identify'), $attributes)
19+
$this->post(route('api.auth.identify'), $payload)
2020
->assertStatus(200)
2121
->assertSee($user->email);
2222
}
@@ -26,15 +26,52 @@ public function a_user_can_be_authenticated()
2626
{
2727
$user = User::first();
2828

29-
$attributes = [
29+
$payload = [
3030
'username' => $user->username,
3131
'password' => 'secret'
3232
];
3333

34-
$this->post(route('api.auth.signin'), $attributes)
34+
$this->post(route('api.auth.signin'), $payload)
3535
->assertStatus(200)
3636
->assertJsonStructure([
3737
'auth_token', 'token_type', 'expires_in'
3838
]);
3939
}
40+
41+
/** @test */
42+
public function a_user_can_view_itself()
43+
{
44+
$payload = array_merge($this->getDefaultPayload(), []);
45+
46+
$this->post(route('api.auth.user'), $payload)
47+
->assertStatus(200)
48+
->assertJson(_test_user()->toArray());
49+
}
50+
51+
/** @test */
52+
public function a_user_can_refresh_its_session()
53+
{
54+
$payload = array_merge($this->getDefaultPayload(), []);
55+
56+
$this->post(route('api.auth.refresh'), $payload)
57+
->assertStatus(200)
58+
->assertJsonStructure([
59+
'auth_token', 'token_type', 'expires_in'
60+
])
61+
->assertJsonMissing([
62+
'auth_token' => $payload['auth_token'],
63+
]);
64+
}
65+
66+
/** @test */
67+
public function a_user_can_signout()
68+
{
69+
$payload = array_merge($this->getDefaultPayload(), []);
70+
71+
$this->post(route('api.auth.signout'), $payload)
72+
->assertStatus(200)
73+
->assertJsonStructure([
74+
'message'
75+
]);
76+
}
4077
}

tests/Feature/Api/BaseTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@
99
abstract class BaseTest extends TestCase
1010
{
1111
use WithFaker, RefreshDatabase;
12+
13+
/**
14+
* Get default request payload
15+
*
16+
* @return array
17+
*/
18+
protected function getDefaultPayload()
19+
{
20+
return [
21+
'auth_token' => _test_user()->auth_token,
22+
];
23+
}
1224
}

tests/Feature/Api/UsersTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,4 @@ public function a_user_can_view_a_user()
4545
->assertStatus(200)
4646
->assertJson($user->toArray());
4747
}
48-
49-
/**
50-
* Get default request payload
51-
*
52-
* @return array
53-
*/
54-
protected function getDefaultPayload()
55-
{
56-
return [
57-
'auth_token' => _test_user()->auth_token,
58-
];
59-
}
6048
}

0 commit comments

Comments
 (0)