Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/AuthTest.php
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public function testInfo(): void | ||||
$this->assertTrue(is_array($json['settings'])); | $this->assertTrue(is_array($json['settings'])); | ||||
$this->assertTrue(is_array($json['aliases'])); | $this->assertTrue(is_array($json['aliases'])); | ||||
$this->assertTrue(!isset($json['access_token'])); | $this->assertTrue(!isset($json['access_token'])); | ||||
// Note: Details of the content are tested in testUserResponse() | // Note: Details of the content are tested in testUserResponse() | ||||
// Test token refresh via the info request | // Test token refresh via the info request | ||||
// First we log in as we need the token (actingAs() will not work) | // First we log in as we need the token (actingAs() will not work) | ||||
$post = ['email' => 'john@kolab.org', 'password' => 'simple123']; | $post = ['email' => 'john@kolab.org', 'password' => \App\Utils::generatePassphrase()]; | ||||
$response = $this->post("api/auth/login", $post); | $response = $this->post("api/auth/login", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response = $this->withHeaders(['Authorization' => 'Bearer ' . $json['access_token']]) | $response = $this->withHeaders(['Authorization' => 'Bearer ' . $json['access_token']]) | ||||
->get("api/auth/info?refresh_token=1"); | ->get("api/auth/info?refresh_token=1"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
Show All 27 Lines | public function testLogin(): string | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame('Invalid username or password.', $json['message']); | $this->assertSame('Invalid username or password.', $json['message']); | ||||
// Valid user+password | // Valid user+password | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$post = ['email' => 'john@kolab.org', 'password' => 'simple123']; | $post = ['email' => 'john@kolab.org', 'password' => \App\Utils::generatePassphrase()]; | ||||
$response = $this->post("api/auth/login", $post); | $response = $this->post("api/auth/login", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertTrue(!empty($json['access_token'])); | $this->assertTrue(!empty($json['access_token'])); | ||||
$this->assertEquals(\config('jwt.ttl') * 60, $json['expires_in']); | $this->assertEquals(\config('jwt.ttl') * 60, $json['expires_in']); | ||||
$this->assertEquals('bearer', $json['token_type']); | $this->assertEquals('bearer', $json['token_type']); | ||||
$this->assertEquals($user->id, $json['id']); | $this->assertEquals($user->id, $json['id']); | ||||
$this->assertEquals($user->email, $json['email']); | $this->assertEquals($user->email, $json['email']); | ||||
$this->assertTrue(is_array($json['statusInfo'])); | $this->assertTrue(is_array($json['statusInfo'])); | ||||
$this->assertTrue(is_array($json['settings'])); | $this->assertTrue(is_array($json['settings'])); | ||||
$this->assertTrue(is_array($json['aliases'])); | $this->assertTrue(is_array($json['aliases'])); | ||||
// Valid user+password (upper-case) | // Valid user+password (upper-case) | ||||
$post = ['email' => 'John@Kolab.org', 'password' => 'simple123']; | $post = ['email' => 'John@Kolab.org', 'password' => \App\Utils::generatePassphrase()]; | ||||
$response = $this->post("api/auth/login", $post); | $response = $this->post("api/auth/login", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertTrue(!empty($json['access_token'])); | $this->assertTrue(!empty($json['access_token'])); | ||||
$this->assertEquals(\config('jwt.ttl') * 60, $json['expires_in']); | $this->assertEquals(\config('jwt.ttl') * 60, $json['expires_in']); | ||||
$this->assertEquals('bearer', $json['token_type']); | $this->assertEquals('bearer', $json['token_type']); | ||||
Show All 40 Lines | public function testRefresh(): void | ||||
$response = $this->post("api/auth/refresh"); | $response = $this->post("api/auth/refresh"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
// Test the same using JSON mode | // Test the same using JSON mode | ||||
$response = $this->json('POST', "api/auth/refresh", []); | $response = $this->json('POST', "api/auth/refresh", []); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
// Login the user to get a valid token | // Login the user to get a valid token | ||||
$post = ['email' => 'john@kolab.org', 'password' => 'simple123']; | $post = ['email' => 'john@kolab.org', 'password' => \App\Utils::generatePassphrase()]; | ||||
$response = $this->post("api/auth/login", $post); | $response = $this->post("api/auth/login", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$token = $json['access_token']; | $token = $json['access_token']; | ||||
// Request with a valid token | // Request with a valid token | ||||
$response = $this->withHeaders(['Authorization' => 'Bearer ' . $token])->post("api/auth/refresh"); | $response = $this->withHeaders(['Authorization' => 'Bearer ' . $token])->post("api/auth/refresh"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
Show All 16 Lines |