@ -3,35 +3,29 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Hash;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class UserRoleTest extends TestCase {
public function test_user_role_is_present() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->get("/api/users/1/roles");
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->get('/api/users/1/roles');
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(
fn (AssertableJson $json) => $json->has(
'roles.0',
fn ($json) =>
$json->where('id', 1)
fn ($json) => $json->where('id', 1)
->where('name', 'Administrator')
->where('slug', 'admin')
->etc()
@ -40,34 +34,29 @@ class UserRoleTest extends TestCase {
}
public function test_assign_role_to_a_user() {
$newUser = User::create([
'name' => 'Test User',
'password' => Hash::make('abcd'),
'email' => 'testuser@hydra.project'
'email' => 'testuser@hydra.project',
]);
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 3]); //assign customer role
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(
fn (AssertableJson $json) => $json->has(
'roles.0',
fn ($json) =>
$json->where('id', 3)
fn ($json) => $json->where('id', 3)
->where('name', 'Customer')
->where('slug', 'customer')
->etc()
@ -78,37 +67,32 @@ class UserRoleTest extends TestCase {
}
public function test_assign_role_multiple_times_to_a_user_should_fail() {
$newUser = User::create([
'name' => 'Test User',
'password' => Hash::make('abcd'),
'email' => 'testuser@hydra.project'
'email' => 'testuser@hydra.project',
]);
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 3]); //assign customer role
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 3]); //again assign customer role
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(
fn (AssertableJson $json) => $json->has(
'roles',
fn ($json) =>
$json->has(1)->etc() //only one role
fn ($json) => $json->has(1)->etc() //only one role
)->etc()
);
@ -116,37 +100,32 @@ class UserRoleTest extends TestCase {
}
public function test_assign_multiple_roles_to_a_user() {
$newUser = User::create([
'name' => 'Test User',
'password' => Hash::make('abcd'),
'email' => 'testuser@hydra.project'
'email' => 'testuser@hydra.project',
]);
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 2]); //assign customer role
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 3]); //again assign customer role
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(
fn (AssertableJson $json) => $json->has(
'roles',
fn ($json) =>
$json->has(2)->etc() //only one role
fn ($json) => $json->has(2)->etc() //only one role
)->etc()
);
@ -154,40 +133,35 @@ class UserRoleTest extends TestCase {
}
public function test_delete_role_from_a_user() {
$newUser = User::create([
'name' => 'Test User',
'password' => Hash::make('abcd'),
'email' => 'testuser@hydra.project'
'email' => 'testuser@hydra.project',
]);
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 2]); //assign customer role
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 3]); //again assign customer role
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->delete("/api/users/{$newUser->id}/roles/3"); //delete
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(
fn (AssertableJson $json) => $json->has(
'roles',
fn ($json) =>
$json->has(1)->etc() //only one role
fn ($json) => $json->has(1)->etc() //only one role
)->etc()
);
@ -195,42 +169,37 @@ class UserRoleTest extends TestCase {
}
public function test_delete_all_roles_from_a_user() {
$newUser = User::create([
'name' => 'Test User',
'password' => Hash::make('abcd'),
'email' => 'testuser@hydra.project'
'email' => 'testuser@hydra.project',
]);
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 2]); //assign customer role
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->post("/api/users/{$newUser->id}/roles", ['role_id' => 3]); //again assign customer role
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->delete("/api/users/{$newUser->id}/roles/3"); //delete
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer '.$this->token)
->delete("/api/users/{$newUser->id}/roles/2"); //delete
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(
fn (AssertableJson $json) => $json->has(
'roles',
fn ($json) =>
$json->has(0)->etc() //only one role
fn ($json) => $json->has(0)->etc() //only one role
)->etc()
);