Skip to content

Commit

Permalink
Updated AdminAuthTest
Browse files Browse the repository at this point in the history
  • Loading branch information
shivishbrahma committed Nov 27, 2023
1 parent 9efb9ea commit 492a39f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 92 deletions.
2 changes: 1 addition & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function definition()
'email' => $this->faker->unique()->safeEmail,
'phone' => $this->faker->phoneNumber,
'password' => Hash::make($this->password_str),
'role' => 'user',
'role' => $this->faker->randomElement(['user', 'admin', 'seller', 'sysadmin']),
'active' => true,
'remember_token' => Str::random(10),
];
Expand Down
54 changes: 29 additions & 25 deletions tests/Feature/AdminAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Tests\Feature;

use App\Models\User;
define('ROUTE_EXPLORE', '/explore');

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
use Tests\Traits\SetupTest;

class AdminAuthTest extends TestCase
{
use RefreshDatabase;
private $sysadmin, $admin, $seller, $customer;
use RefreshDatabase, SetupTest;
private Collection $users;

/**
* Setup for the Admin Auth Test
Expand All @@ -20,10 +22,7 @@ class AdminAuthTest extends TestCase
public function setUp(): void
{
parent::setUp();
$this->sysadmin = User::factory()->sysadmin()->create();
$this->admin = User::factory()->admin()->create();
$this->seller = User::factory()->seller()->create();
$this->customer = User::factory()->create();
$this->setUpUsers();
}

/**
Expand All @@ -39,7 +38,7 @@ protected function testAdminSingleGetRoute(
$url,
$status = [],
$redirectUri = ["guest" => '/login'],
$fromUri = '/explore'
$fromUri = ROUTE_EXPLORE
) {
$default_status = ["guest" => 302, "customer" => 403, "seller" => 403, "admin" => 200, "sysadmin" => 200];
foreach ($status as $user_role => $status_code) {
Expand All @@ -48,32 +47,37 @@ protected function testAdminSingleGetRoute(
$status = $default_status;

$response = $this->from($fromUri)->get($url);
if ($response->status() == 302)
if ($response->status() == 302) {
$response->assertRedirect($redirectUri["guest"]);
}

$response = $this->from($fromUri)->actingAs($this->customer)->get($url);
if ($response->status() == 302)
$response = $this->from($fromUri)->actingAs($this->users[0])->get($url);
if ($response->status() == 302) {
$response->assertRedirect($redirectUri["customer"]);
else
} else {
$response->assertStatus($status["customer"]);
}

$response = $this->from($fromUri)->actingAs($this->seller)->get($url);
if ($response->status() == 302)
$response = $this->from($fromUri)->actingAs($this->users[1])->get($url);
if ($response->status() == 302) {
$response->assertRedirect($redirectUri["seller"]);
else
} else {
$response->assertStatus($status["seller"]);
}

$response = $this->from($fromUri)->actingAs($this->admin)->get($url);
if ($response->status() == 302)
$response = $this->from($fromUri)->actingAs($this->users[2])->get($url);
if ($response->status() == 302) {
$response->assertRedirect($redirectUri["admin"]);
else
} else {
$response->assertStatus($status["admin"]);
}

$response = $this->from($fromUri)->actingAs($this->sysadmin)->get($url);
if ($response->status() == 302)
$response = $this->from($fromUri)->actingAs($this->users[3])->get($url);
if ($response->status() == 302) {
$response->assertRedirect($redirectUri["sysadmin"]);
else
} else {
$response->assertStatus($status["sysadmin"]);
}
}

/**
Expand All @@ -99,9 +103,9 @@ public function testAdminRouteRegisterView()
"seller" => 302,
];
$redirectUri = [
"seller" => '/explore',
"admin" => '/explore',
"sysadmin" => '/explore',
"seller" => ROUTE_EXPLORE,
"admin" => ROUTE_EXPLORE,
"sysadmin" => ROUTE_EXPLORE,
"guest" => '/login'
];
$this->testAdminSingleGetRoute(route("admin.register.view"), $status, $redirectUri);
Expand Down
25 changes: 25 additions & 0 deletions tests/Traits/SetupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Traits;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Sequence;

trait SetupTest
{
protected function setUpUsers()
{
// Create test users
$this->users = User::factory()
->count(4)
->state(
new Sequence(
['role' => 'user'],
['role' => 'seller'],
['role' => 'admin'],
['role' => 'sysadmin'],
)
)
->create();
}
}
131 changes: 65 additions & 66 deletions tests/Unit/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

use App\Models\Approval;
use App\Models\User;
use Faker\Generator as Faker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;


Expand All @@ -19,14 +16,17 @@ class AdminTest extends TestCase
*
* @return void
*/
use WithFaker;
use WithFaker, RefreshDatabase;

private $sysadmin, $admin, $seller, $customer;

public function setUp(): void
{
parent::setUp();
$this->sysadmin = User::where("id", "=", 1)->first();
$this->admin = User::where("id", "=", 2)->first();
$this->seller = User::where("id", "=", 11)->first();
$this->customer = User::where("id", "=", 8)->first();
$this->sysadmin = User::factory()->sysadmin()->create();
$this->admin = User::factory()->admin()->create();
$this->seller = User::factory()->seller()->create();
$this->customer = User::factory()->create();
$this->setUpFaker();
}

Expand All @@ -42,71 +42,70 @@ public function test_admin_register()
$this->assertEquals($lastApproval->user_id, $this->customer->id);
}

public function test_approval()
{
$lastApproval1 = Approval::all()->sortByDesc('updated_at')->first();
$approve_data = ['input' => 'approve', 'id' => $lastApproval1->id];
$admin_obj = [
'user_id' => $this->customer->id,
'type' => 'admin_approval',
];
$seller_obj = [
'user_id' => $this->customer->id,
'type' => 'seller_approval',
];

$response1 = $this->actingAs($this->admin)->post(route('admin.approval'), $approve_data);
$c1 = User::all()
->where('id', '=', $lastApproval1->user_id)->first();
$this->assertEquals($c1->role, 'admin');
$c1->role = 'customer';
$c1->save();

$declineApproval = Approval::create($admin_obj);
$decline_data = ['input' => 'decline', 'id' => $declineApproval->id];
$response2 = $this->actingAs($this->admin)->post(route('admin.approval'), $decline_data);

$c2 = User::all()
->where('id', '=', $declineApproval->user_id)->first();
$this->assertEquals($c2->role, 'customer');
$c2->role = 'customer';
$c2->save();

$req = [
'name' => 'TestSeller',
'gstin' => '6512r65',
'aadhaar' => '333311114444',
'trade_name' => 'UnitTestingSeller'
];
$response = $this->actingAs($this->customer)->post(route('seller.register'), $req);
$sellerApproval = Approval::all()->sortByDesc('updated_at')->first();
$seller_data = ['input' => 'approve', 'id' => $sellerApproval->id];
$response3 = $this->actingAs($this->admin)->post(route('admin.approval'), $seller_data);

$seller = User::all()
->where('id', '=', $sellerApproval->user_id)->first();
$this->assertEquals($seller->role, 'seller');
$seller->role = 'customer';
$seller->save();

$response1->assertStatus(302);
$response1->assertRedirect(route('admin.approval.view'));

$response2->assertStatus(302);
$response2->assertRedirect(route('admin.approval.view'));

$response3->assertStatus(302);
$response3->assertRedirect(route('admin.approval.view'));
}
// public function test_approval()
// {
// $lastApproval1 = Approval::all()->sortByDesc('updated_at')->first();
// $approve_data = ['input' => 'approve', 'id' => $lastApproval1->id];
// $admin_obj = [
// 'user_id' => $this->customer->id,
// 'type' => 'admin_approval',
// ];
// $seller_obj = [
// 'user_id' => $this->customer->id,
// 'type' => 'seller_approval',
// ];

// $response1 = $this->actingAs($this->admin)->post(route('admin.approval'), $approve_data);
// $c1 = User::all()
// ->where('id', '=', $lastApproval1->user_id)->first();
// $this->assertEquals($c1->role, 'admin');
// $c1->role = 'customer';
// $c1->save();

// $declineApproval = Approval::create($admin_obj);
// $decline_data = ['input' => 'decline', 'id' => $declineApproval->id];
// $response2 = $this->actingAs($this->admin)->post(route('admin.approval'), $decline_data);

// $c2 = User::all()
// ->where('id', '=', $declineApproval->user_id)->first();
// $this->assertEquals($c2->role, 'customer');
// $c2->role = 'customer';
// $c2->save();

// $req = [
// 'name' => 'TestSeller',
// 'gstin' => '6512r65',
// 'aadhaar' => '333311114444',
// 'trade_name' => 'UnitTestingSeller'
// ];
// $response = $this->actingAs($this->customer)->post(route('seller.register'), $req);
// $sellerApproval = Approval::all()->sortByDesc('updated_at')->first();
// $seller_data = ['input' => 'approve', 'id' => $sellerApproval->id];
// $response3 = $this->actingAs($this->admin)->post(route('admin.approval'), $seller_data);

// $seller = User::all()
// ->where('id', '=', $sellerApproval->user_id)->first();
// $this->assertEquals($seller->role, 'seller');
// $seller->role = 'customer';
// $seller->save();

// $response1->assertStatus(302);
// $response1->assertRedirect(route('admin.approval.view'));

// $response2->assertStatus(302);
// $response2->assertRedirect(route('admin.approval.view'));

// $response3->assertStatus(302);
// $response3->assertRedirect(route('admin.approval.view'));
// }

public function test_admin_browse()
{
$data = ['input' => $this->admin->id];

$response = $this->actingAs($this->sysadmin)->post(route('admin.browse'), $data);

$c1 = User::all()
->where('id', '=', $this->admin->id)->first();
$c1 = User::where('id', '=', $this->admin->id)->first();

$this->assertEquals($c1->role, 'customer');
$c1->role = 'admin';
Expand Down

0 comments on commit 492a39f

Please sign in to comment.