Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use App\User;
- use Illuminate\Foundation\Testing\WithoutMiddleware;
- use Illuminate\Foundation\Testing\DatabaseMigrations;
- use Illuminate\Foundation\Testing\DatabaseTransactions;
- class AuthTest extends TestCase
- {
- use DatabaseTransactions;
- protected $pages = [
- 'login' => 'login',
- 'admin' => 'admin',
- 'dash' => 'home',
- 'api' => 'api'
- ];
- protected $userSeeJson = [
- 'id',
- 'name',
- 'email',
- 'created_at'
- ];
- protected function createUser($override = [], $type = null)
- {
- return factory(User::class, $type)->create($override);
- }
- protected function faker()
- {
- return Faker\Factory::create();
- }
- public function testRedirectToLoginWhenNotAuthenticated()
- {
- $this->visit($this->pages['dash'])
- ->seePageIs($this->pages['login']);
- }
- public function testLogin()
- {
- $user = $this->createUser(['password' => bcrypt($password = str_random(10))]);
- $this->visit($this->pages['login'])
- ->type($user->email, 'email')
- ->type($password, 'password')
- ->press('Login');
- $this->seePageIs($this->pages['dash']);
- }
- public function testFailedLogin()
- {
- $faker = $this->faker();
- $this->visit($this->pages['login'])
- ->type($faker->email, 'email')
- ->type($faker->password, 'password')
- ->press('Login');
- $this->seePageIs($this->pages['login']);
- $this->see(trans('auth.failed'));
- }
- public function testRedirectAwayFromLogin()
- {
- $user = $this->createUser();
- $this->actingAs($user)
- ->visit($this->pages['login'])
- ->seePageIs($this->pages['dash']);
- }
- public function testAuthUserCanReachDash()
- {
- $user = $this->createUser();
- $path = $this->pages['dash'];
- $this->actingAs($user)
- ->visit($path)
- ->seePageIs($path);
- }
- // Admin
- public function testAdminCanMakeItToAdmin()
- {
- $user = $this->createUser([], 'admin');
- $path = $this->pages['admin'];
- $this->actingAs($user)
- ->visit($path)
- ->seePageIs($path);
- }
- public function testNonAdminsGetRedirectedAwayFromAdmin()
- {
- $user = $this->createUser();
- $this->actingAs($user)
- ->visit($this->pages['admin'])
- ->seePageIs($this->pages['dash']);
- }
- // API
- public function testApiGuard()
- {
- $user = $this->createUser();
- $path = $this->pages['api'];
- $this->makeRequest('GET', $path, ['api_token' => $user->api_token])
- ->seeJsonStructure($this->userSeeJson);
- $this->visit($path .'?api_token='. $user->api_token)
- ->assertResponseStatus('200');
- $this->get($path, ['PHP_AUTH_PW' => $user->api_token])
- ->seeJsonStructure($this->userSeeJson);
- $this->get($path, ['Authorization' => 'Bearer '. $user->api_token])
- ->seeJsonStructure($this->userSeeJson);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement