-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
AuthTest.php
81 lines (72 loc) · 2 KB
/
AuthTest.php
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
<?php
namespace Tests\Feature\V1\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function user_can_retrieve_a_jwt()
{
$this->anakin();
$this->json('POST', '/api/v1/auth/login', [
'email' => 'anakin@skywalker.st',
'password' => '4nak1n'
])
->assertStatus(200)
->assertJsonStructure([
'access_token',
'token_type',
'expires_in',
'user_id',
])
->assertJson([
'token_type' => 'bearer',
'expires_in' => 3600
]);
}
/** @test */
public function user_cannot_retrieve_a_jwt()
{
$this->json('POST', '/api/v1/auth/login', [
'email' => 'anakin@skywalker.st',
'password' => 'Luk3'
])
->assertStatus(401)
->assertJson([
'errors' => [
'email' => ['These credentials do not match our records.']
]
]);
}
/** @test */
public function user_can_be_authenticated_with_jwt()
{
$anakin = $this->anakin();
$anakin->wasRecentlyCreated = false;
$this->actingAs($anakin)
->json('GET', '/api/v1/auth/me')
->assertStatus(200)
->assertJsonStructure([
'data' => [
'name',
'email',
]
])
->assertJson([
'data' => [
'name' => 'Anakin',
'email' => 'anakin@skywalker.st'
]
]);
}
/** @test */
public function user_must_be_authenticated()
{
$this->json('GET', '/api/v1/auth/me')
->assertStatus(401)
->assertJson([
'message' => 'Unauthenticated.'
]);
}
}