Advertisement
Guest User

Untitled

a guest
Jan 24th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <?php
  2.  
  3. use App\User;
  4. use Illuminate\Foundation\Testing\WithoutMiddleware;
  5. use Illuminate\Foundation\Testing\DatabaseMigrations;
  6. use Illuminate\Foundation\Testing\DatabaseTransactions;
  7.  
  8. class AuthTest extends TestCase
  9. {
  10. use DatabaseTransactions;
  11.  
  12. protected $pages = [
  13. 'login' => 'login',
  14. 'admin' => 'admin',
  15. 'dash' => 'home',
  16. 'api' => 'api'
  17. ];
  18.  
  19. protected $userSeeJson = [
  20. 'id',
  21. 'name',
  22. 'email',
  23. 'created_at'
  24. ];
  25.  
  26. protected function createUser($override = [], $type = null)
  27. {
  28. return factory(User::class, $type)->create($override);
  29. }
  30.  
  31. protected function faker()
  32. {
  33. return Faker\Factory::create();
  34. }
  35.  
  36.  
  37. public function testRedirectToLoginWhenNotAuthenticated()
  38. {
  39. $this->visit($this->pages['dash'])
  40. ->seePageIs($this->pages['login']);
  41. }
  42.  
  43. public function testLogin()
  44. {
  45. $user = $this->createUser(['password' => bcrypt($password = str_random(10))]);
  46.  
  47. $this->visit($this->pages['login'])
  48. ->type($user->email, 'email')
  49. ->type($password, 'password')
  50. ->press('Login');
  51.  
  52. $this->seePageIs($this->pages['dash']);
  53. }
  54.  
  55. public function testFailedLogin()
  56. {
  57. $faker = $this->faker();
  58.  
  59. $this->visit($this->pages['login'])
  60. ->type($faker->email, 'email')
  61. ->type($faker->password, 'password')
  62. ->press('Login');
  63.  
  64. $this->seePageIs($this->pages['login']);
  65. $this->see(trans('auth.failed'));
  66. }
  67.  
  68. public function testRedirectAwayFromLogin()
  69. {
  70. $user = $this->createUser();
  71.  
  72. $this->actingAs($user)
  73. ->visit($this->pages['login'])
  74. ->seePageIs($this->pages['dash']);
  75. }
  76.  
  77. public function testAuthUserCanReachDash()
  78. {
  79. $user = $this->createUser();
  80.  
  81. $path = $this->pages['dash'];
  82.  
  83. $this->actingAs($user)
  84. ->visit($path)
  85. ->seePageIs($path);
  86. }
  87.  
  88. // Admin
  89. public function testAdminCanMakeItToAdmin()
  90. {
  91. $user = $this->createUser([], 'admin');
  92.  
  93. $path = $this->pages['admin'];
  94.  
  95. $this->actingAs($user)
  96. ->visit($path)
  97. ->seePageIs($path);
  98. }
  99.  
  100. public function testNonAdminsGetRedirectedAwayFromAdmin()
  101. {
  102. $user = $this->createUser();
  103.  
  104. $this->actingAs($user)
  105. ->visit($this->pages['admin'])
  106. ->seePageIs($this->pages['dash']);
  107. }
  108.  
  109. // API
  110. public function testApiGuard()
  111. {
  112. $user = $this->createUser();
  113.  
  114. $path = $this->pages['api'];
  115.  
  116. $this->makeRequest('GET', $path, ['api_token' => $user->api_token])
  117. ->seeJsonStructure($this->userSeeJson);
  118.  
  119. $this->visit($path .'?api_token='. $user->api_token)
  120. ->assertResponseStatus('200');
  121.  
  122. $this->get($path, ['PHP_AUTH_PW' => $user->api_token])
  123. ->seeJsonStructure($this->userSeeJson);
  124.  
  125. $this->get($path, ['Authorization' => 'Bearer '. $user->api_token])
  126. ->seeJsonStructure($this->userSeeJson);
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement