<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

use App\Models\User;
use App\Models\Domain;

class SiteAdminTest extends TestCase
{
	use DatabaseTransactions;

	/**
	 * Testing a unauthenticated visitor to the site.
	 */
	public function test_guestuser()
	{
		$this->get('about')
			->assertOk();

		$this->get('domain')
			->assertRedirect('login');
		$this->get('zone')
			->assertRedirect('login');

		$this->get('domain/view/999')
			->assertNotFound();

		Domain::factory()->create([
			'id'=>999,
			'name'=>'test',
			'public'=>TRUE,
		]);

		$this->get('domain/view/999')
			->assertOK();
	}

	/**
	 * Verify user who has registered by not verified their email
	 */
	public function test_unverified_user()
	{
		$user = User::factory()->make([
			'name'=>'Site Visitor',
			'email_verified_at'=>NULL,
		]);

		// Use model in tests...
		$this->actingAs($user);

		$this->get('domain')
			->assertRedirect('email/verify');
		$this->get('zone')
			->assertRedirect('email/verify');

		Auth::logout();
	}

	public function test_site_admin()
	{
		// Site Visitor
		$this->get('setup')
			->assertRedirect('login');

		// Valid User
		$user = User::factory()->make([
			'name'=>'Site User',
		]);
		$this->actingAs($user);

		$this->get('setup')
			->assertForbidden();

		// Admin User
		$user = User::factory()->admin()->make([
			'name'=>'Site User',
		]);
		$this->actingAs($user);

		$this->get('setup')
			->assertOK();
	}
}