pull/3/head
Hasin Hayder 3 years ago
parent bb6cece73e
commit eab75c8720

@ -1,32 +0,0 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Testing\Fluent\AssertableJson;
class AdminLoginFailTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
$response = $this->postJson('/api/login',[
'email'=>'admin@hydra.project',
'password'=>'hydrax'
]);
$response
->assertJson(fn (AssertableJson $json) =>
$json->where('error', 1)
->missing('token')
->has('message')
);
}
}

@ -15,7 +15,7 @@ class AdminLoginTest extends TestCase
* *
* @return void * @return void
*/ */
public function test_example() public function test_admin_login()
{ {
$response = $this->postJson('/api/login',[ $response = $this->postJson('/api/login',[
'email'=>'admin@hydra.project', 'email'=>'admin@hydra.project',
@ -29,4 +29,19 @@ class AdminLoginTest extends TestCase
->etc() ->etc()
); );
} }
public function test_admin_login_fail()
{
$response = $this->postJson('/api/login',[
'email'=>'admin@hydra.project',
'password'=>'hydrax'
]);
$response
->assertJson(fn (AssertableJson $json) =>
$json->where('error', 1)
->missing('token')
->has('message')
);
}
} }

@ -1,21 +0,0 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_the_application_returns_a_successful_response()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

@ -0,0 +1,246 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Testing\Fluent\AssertableJson;
use App\Models\Role;
class RoleTest extends TestCase {
/**
* A basic feature test example.
*
* @return void
*/
public function test_list_roles() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'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/roles");
$response
->assertJson(
fn (AssertableJson $json) =>
$json->has(6)
->first(
fn ($json) =>
$json->where('id', 1)
->where('name', 'Administrator')
->where('slug','admin')
->etc()
)
);
}
public function test_update_role_name_as_admin() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/4",[
"name"=>"Chief Editor"
]);
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('name','Chief Editor')
->missing('error')
->etc()
);
}
public function test_update_role_slug_as_admin() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/4",[
"slug"=>"chief-editor"
]);
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('slug','chief-editor')
->missing('error')
->etc()
);
}
public function test_update_role_namd_and_slug_as_admin() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/4",[
"name"=>"Editor X",
"slug"=>"editor-x"
]);
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('name','Editor X')
->where('slug','editor-x')
->missing('error')
->etc()
);
}
public function test_update_admin_slug_as_admin_should_fail() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/1",[
"slug"=>"admin-x"
]);
$response
->assertJson(
fn (AssertableJson $json) =>
$json
->where('slug','admin')
->etc()
);
}
public function test_create_new_role_as_admin() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->post("/api/roles",[
"name"=>"New Role",
"slug"=>"new-role"
]);
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('name','New Role')
->where('slug','new-role')
->missing('error')
->etc()
);
}
public function test_duplicate_role_will_not_be_created() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->post("/api/roles",[
"name"=>"New Role",
"slug"=>"new-role"
]);
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('error',1)
->etc()
);
}
public function test_delete_role_as_admin() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$newRole = Role::where('slug','new-role')->first();
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->delete("/api/roles/{$newRole->id}");
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('error',0)
->has('message')
);
}
public function test_delete_admin_role_should_fail() {
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'password' => 'hydra'
]);
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->user_id = $data->id;
$newRole = Role::where('slug','admin')->first();
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->delete("/api/roles/{$newRole->id}");
$response
->assertJson(
fn (AssertableJson $json) =>
$json->where('error',1)
->has('message')
);
}
}
Loading…
Cancel
Save