|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Passport\Tests\Feature; |
| 4 | + |
| 5 | +use Javaabu\Passport\Tests\TestSupport\Models\User; |
| 6 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 7 | +use Javaabu\Passport\Tests\TestSupport\Enums\UserStatuses; |
| 8 | +use Javaabu\Passport\Tests\TestCase; |
| 9 | +use Laravel\Passport\ClientRepository; |
| 10 | + |
| 11 | +class OAuthControllerTest extends TestCase |
| 12 | +{ |
| 13 | + use RefreshDatabase; |
| 14 | + |
| 15 | + public function test_it_can_generate_a_client_credentials_access_token() |
| 16 | + { |
| 17 | + $user = $this->getUser(); |
| 18 | + $client = (new ClientRepository())->createClientCredentialsGrantClient( |
| 19 | + 'Test Client', |
| 20 | + ); |
| 21 | + |
| 22 | + $this->json('post', '/oauth/token', [ |
| 23 | + 'client_id' => $client->id, |
| 24 | + 'client_secret' => $client->plainSecret, |
| 25 | + 'grant_type' => 'client_credentials', |
| 26 | + 'scope' => '*', |
| 27 | + ]) |
| 28 | + ->assertStatus(200) |
| 29 | + ->assertJsonStructure([ |
| 30 | + 'token_type', |
| 31 | + 'expires_in', |
| 32 | + 'access_token', |
| 33 | + ]) |
| 34 | + ->assertJson([ |
| 35 | + 'token_type' => 'Bearer', |
| 36 | + ]); |
| 37 | + } |
| 38 | + |
| 39 | + public function test_it_can_generate_a_password_grant_access_token_for_an_admin() |
| 40 | + { |
| 41 | + $user = $this->getUser(); |
| 42 | + $client = (new ClientRepository())->createPasswordGrantClient( |
| 43 | + 'Test Client', |
| 44 | + 'users', |
| 45 | + ); |
| 46 | + |
| 47 | + $this->json('post', '/oauth/token', [ |
| 48 | + 'client_id' => $client->id, |
| 49 | + 'client_secret' => $client->plainSecret, |
| 50 | + 'grant_type' => 'password', |
| 51 | + 'username' => $user->email, |
| 52 | + 'password' => 'password', |
| 53 | + 'scope' => '*', |
| 54 | + ]) |
| 55 | + ->assertStatus(200) |
| 56 | + ->assertJsonStructure([ |
| 57 | + 'token_type', |
| 58 | + 'expires_in', |
| 59 | + 'access_token', |
| 60 | + 'refresh_token', |
| 61 | + ]) |
| 62 | + ->assertJson([ |
| 63 | + 'token_type' => 'Bearer', |
| 64 | + ]); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_it_wont_grant_a_password_access_token_for_an_admin_with_incorrect_password() |
| 68 | + { |
| 69 | + $user = $this->getUser(); |
| 70 | + $client = (new ClientRepository())->createPasswordGrantClient( |
| 71 | + 'Test Client', |
| 72 | + 'users' |
| 73 | + ); |
| 74 | + |
| 75 | + $this->json('post', '/oauth/token', [ |
| 76 | + 'client_id' => $client->id, |
| 77 | + 'client_secret' => $client->plainSecret, |
| 78 | + 'grant_type' => 'password', |
| 79 | + 'username' => $user->email, |
| 80 | + 'password' => 'wrong password', |
| 81 | + 'scope' => '*', |
| 82 | + ]) |
| 83 | + ->assertStatus(400) |
| 84 | + ->assertJson([ |
| 85 | + 'error' => 'invalid_grant', |
| 86 | + ]); |
| 87 | + } |
| 88 | + |
| 89 | + public function test_it_can_authorize_an_admin() |
| 90 | + { |
| 91 | + $this->withoutExceptionHandling(); |
| 92 | + |
| 93 | + $user = $this->getUser(); |
| 94 | + $this->actingAsApiUser($user); |
| 95 | + |
| 96 | + $this->json('get', '/users/profile') |
| 97 | + ->assertStatus(200) |
| 98 | + ->assertJson([ |
| 99 | + 'name' => $user->name, |
| 100 | + ]); |
| 101 | + } |
| 102 | + |
| 103 | + public function test_it_wont_allow_an_inactive_admin() |
| 104 | + { |
| 105 | + $user = $this->getUser(); |
| 106 | + $user->status = UserStatuses::PENDING; |
| 107 | + $user->save(); |
| 108 | + |
| 109 | + $this->actingAsApiUser($user); |
| 110 | + |
| 111 | + $this->json('get', '/users/profile') |
| 112 | + ->assertStatus(403) |
| 113 | + ->assertJson([ |
| 114 | + 'message' => 'Account not activated', |
| 115 | + ]); |
| 116 | + |
| 117 | + $this->json('get', '/test') |
| 118 | + ->assertStatus(403) |
| 119 | + ->assertJson([ |
| 120 | + 'message' => 'Account not activated', |
| 121 | + ]); |
| 122 | + } |
| 123 | + |
| 124 | + public function test_it_can_authorize_a_client_access_token_from_auth_header() |
| 125 | + { |
| 126 | + $access_token = $this->getClientAccessToken(); |
| 127 | + |
| 128 | + $this->json('get', '/test', [], [ |
| 129 | + 'Authorization' => "Bearer $access_token", |
| 130 | + ]) |
| 131 | + ->assertStatus(200) |
| 132 | + ->assertJsonFragment([ |
| 133 | + 'It works', |
| 134 | + ]); |
| 135 | + } |
| 136 | + |
| 137 | + public function test_it_can_authorize_a_user_from_an_auth_token_cookie() |
| 138 | + { |
| 139 | + $user = $this->getUser(); |
| 140 | + $access_cookie = $this->getOAuthCookie($user); |
| 141 | + |
| 142 | + // check if it doesn't work without an auth cookie |
| 143 | + $this->json('get', '/users/profile') |
| 144 | + ->assertStatus(401) |
| 145 | + ->assertDontSee($user->name); |
| 146 | + |
| 147 | + $this->jsonApi('get', '/users/profile', [], $access_cookie) |
| 148 | + ->assertStatus(200) |
| 149 | + ->assertJson([ |
| 150 | + 'name' => $user->name, |
| 151 | + ]); |
| 152 | + } |
| 153 | + |
| 154 | + public function test_it_can_authorize_client_credentials_from_an_auth_token_cookie() |
| 155 | + { |
| 156 | + $access_cookie = $this->getOAuthCookie(null); |
| 157 | + |
| 158 | + $this->jsonApi('get', '/test', [], $access_cookie) |
| 159 | + ->assertStatus(200) |
| 160 | + ->assertJsonFragment([ |
| 161 | + 'It works', |
| 162 | + ]); |
| 163 | + } |
| 164 | + |
| 165 | + public function test_it_generates_a_valid_token_cookie_for_inactive_users() |
| 166 | + { |
| 167 | + $user = $this->getUser(); |
| 168 | + $user->status = UserStatuses::PENDING; |
| 169 | + $user->save(); |
| 170 | + |
| 171 | + $this->actingAs($user, 'web'); |
| 172 | + |
| 173 | + $access_cookie = $this->get('/verify') |
| 174 | + ->headers |
| 175 | + ->getCookies()[0]; |
| 176 | + |
| 177 | + // check if it doesn't work without an auth cookie |
| 178 | + $this->json('get', '/test') |
| 179 | + ->assertStatus(401) |
| 180 | + ->assertDontSee('It works'); |
| 181 | + |
| 182 | + // make sure can't access active routes |
| 183 | + $this->jsonApi('get', '/users/profile', [], $access_cookie->getValue()) |
| 184 | + ->assertStatus(401) |
| 185 | + ->assertDontSee($user->name); |
| 186 | + |
| 187 | + $this->jsonApi('get', '/test', [], $access_cookie->getValue()) |
| 188 | + ->assertStatus(200) |
| 189 | + ->assertJsonFragment([ |
| 190 | + 'It works', |
| 191 | + ]); |
| 192 | + } |
| 193 | + |
| 194 | + public function test_it_generates_a_valid_token_cookie_for_active_users() |
| 195 | + { |
| 196 | + $user = $this->getUser(); |
| 197 | + |
| 198 | + $this->actingAs($user, 'web'); |
| 199 | + |
| 200 | + $access_cookie = $this->get('/dashboard') |
| 201 | + ->assertStatus(200) |
| 202 | + ->headers |
| 203 | + ->getCookies()[0]; |
| 204 | + |
| 205 | + // check if it doesn't work without an auth cookie |
| 206 | + $this->json('get', '/test') |
| 207 | + ->assertStatus(401) |
| 208 | + ->assertDontSee('It works'); |
| 209 | + |
| 210 | + //make sure can't access active routes |
| 211 | + $this->jsonApi('get', '/users/profile', [], $access_cookie->getValue()) |
| 212 | + ->assertStatus(200) |
| 213 | + ->assertJsonFragment([ |
| 214 | + 'name' => $user->name, |
| 215 | + ]); |
| 216 | + |
| 217 | + $this->jsonApi('get', '/test', [], $access_cookie->getValue()) |
| 218 | + ->assertStatus(200) |
| 219 | + ->assertJsonFragment([ |
| 220 | + 'It works', |
| 221 | + ]); |
| 222 | + } |
| 223 | +} |
0 commit comments