2018-10-24 12:24:09 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-24 12:24:09 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2018-10-24 12:24:09 +02:00
|
|
|
use App\Models\Account;
|
2019-01-16 10:28:06 +01:00
|
|
|
use App\Models\Client;
|
2020-10-01 13:34:05 +02:00
|
|
|
use App\Models\Company;
|
2019-09-11 04:01:49 +02:00
|
|
|
use App\Models\CompanyToken;
|
2018-10-24 12:24:09 +02:00
|
|
|
use App\Models\User;
|
2019-01-16 10:28:06 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2018-10-24 12:24:09 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-01-07 12:30:28 +01:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
2019-01-16 10:28:06 +01:00
|
|
|
use Illuminate\Http\Request;
|
2020-03-09 10:38:15 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-10-26 06:53:29 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2018-10-24 12:24:09 +02:00
|
|
|
use Tests\TestCase;
|
2020-11-01 06:09:09 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2018-10-24 12:24:09 +02:00
|
|
|
|
2019-04-20 01:02:49 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\Auth\LoginController
|
|
|
|
*/
|
2018-10-24 12:24:09 +02:00
|
|
|
class LoginTest extends TestCase
|
|
|
|
{
|
2019-04-24 02:22:02 +02:00
|
|
|
use DatabaseTransactions;
|
2018-10-24 12:24:09 +02:00
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function setUp() :void
|
2018-10-26 06:53:29 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-10-29 05:24:12 +01:00
|
|
|
Session::start();
|
2018-10-26 06:53:29 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
public function testLoginFormDisplayed()
|
|
|
|
{
|
2018-10-29 05:24:12 +01:00
|
|
|
$response = $this->get('/login', [
|
2020-09-06 11:38:10 +02:00
|
|
|
'_token' => csrf_token(),
|
2018-10-29 05:24:12 +01:00
|
|
|
]);
|
|
|
|
|
2020-02-15 10:06:30 +01:00
|
|
|
$response->assertStatus(404);
|
2018-10-24 12:24:09 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
/**
|
|
|
|
* A valid user can be logged in.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-02-15 10:06:30 +01:00
|
|
|
// public function testLoginAValidUser()
|
|
|
|
// {
|
|
|
|
// $account = factory(Account::class)->create();
|
|
|
|
// $user = factory(User::class)->create([
|
|
|
|
// // 'account_id' => $account->id,
|
|
|
|
// ]);
|
2020-10-01 12:49:47 +02:00
|
|
|
// $company = Company::factory()->make([
|
2020-02-15 10:06:30 +01:00
|
|
|
// 'account_id' => $account->id,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $user->companies()->attach($company->id, [
|
|
|
|
// 'account_id' => $account->id,
|
|
|
|
// 'is_owner' => 1,
|
|
|
|
// 'is_admin' => 1,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $response = $this->post('/login', [
|
|
|
|
// 'email' => config('ninja.testvars.username'),
|
|
|
|
// 'password' => config('ninja.testvars.password'),
|
|
|
|
// '_token' => csrf_token()
|
|
|
|
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// //$response->assertStatus(302);
|
|
|
|
// $this->assertAuthenticatedAs($user);
|
|
|
|
// }
|
2018-10-24 12:24:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An invalid user cannot be logged in.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-02-15 10:06:30 +01:00
|
|
|
// public function testDoesNotLoginAnInvalidUser()
|
|
|
|
// {
|
|
|
|
// $account = factory(Account::class)->create();
|
|
|
|
// $user = factory(User::class)->create([
|
|
|
|
// // 'account_id' => $account->id,
|
|
|
|
// ]);
|
2020-10-01 12:49:47 +02:00
|
|
|
// $company = Company::factory()->make([
|
2020-02-15 10:06:30 +01:00
|
|
|
// 'account_id' => $account->id,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $user->companies()->attach($company->id, [
|
|
|
|
// 'account_id' => $account->id,
|
|
|
|
// 'is_owner' => 1,
|
|
|
|
// 'is_admin' => 1,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $response = $this->post('/login', [
|
|
|
|
// 'email' => config('ninja.testvars.username'),
|
|
|
|
// 'password' => 'invaliddfd',
|
|
|
|
// '_token' => csrf_token()
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// //$response->assertSessionHasErrors();
|
|
|
|
// $this->assertGuest();
|
|
|
|
// }
|
|
|
|
// /**
|
|
|
|
// * A logged in user can be logged out.
|
|
|
|
// *
|
|
|
|
// * @return void
|
|
|
|
// */
|
|
|
|
// public function testLogoutAnAuthenticatedUser()
|
|
|
|
// {
|
|
|
|
// $account = factory(Account::class)->create();
|
|
|
|
// $user = factory(User::class)->create([
|
|
|
|
// // 'account_id' => $account->id,
|
|
|
|
// ]);
|
2020-10-01 12:49:47 +02:00
|
|
|
// $company = Company::factory()->make([
|
2020-02-15 10:06:30 +01:00
|
|
|
// 'account_id' => $account->id,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $user->companies()->attach($company->id, [
|
|
|
|
// 'account_id' => $account->id,
|
|
|
|
// 'is_owner' => 1,
|
|
|
|
// 'is_admin' => 1,
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $response = $this->actingAs($user)->post('/logout',[
|
|
|
|
// '_token' => csrf_token()
|
|
|
|
// ]);
|
|
|
|
// $response->assertStatus(302);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-15 10:06:30 +01:00
|
|
|
// // $this->assertGuest();
|
|
|
|
// }
|
2019-01-16 10:28:06 +01:00
|
|
|
|
2019-04-19 10:09:05 +02:00
|
|
|
public function testApiLogin()
|
|
|
|
{
|
2020-11-01 06:09:09 +01:00
|
|
|
Account::all()->each(function ($account){
|
|
|
|
$account->delete();
|
|
|
|
});
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$account = Account::factory()->create();
|
|
|
|
$user = User::factory()->create([
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account->id,
|
2019-04-19 10:09:05 +02:00
|
|
|
'email' => 'test@example.com',
|
2020-09-06 11:38:10 +02:00
|
|
|
'password' => \Hash::make('123456'),
|
2019-04-19 10:09:05 +02:00
|
|
|
]);
|
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$company = Company::factory()->create([
|
2019-04-19 10:09:05 +02:00
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$account->default_company_id = $company->id;
|
2019-09-11 04:01:49 +02:00
|
|
|
$account->save();
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token = new CompanyToken;
|
|
|
|
$company_token->user_id = $user->id;
|
|
|
|
$company_token->company_id = $company->id;
|
|
|
|
$company_token->account_id = $account->id;
|
2020-09-06 11:38:10 +02:00
|
|
|
$company_token->name = $user->first_name.' '.$user->last_name;
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token->token = \Illuminate\Support\Str::random(64);
|
|
|
|
$company_token->save();
|
2019-09-11 04:01:49 +02:00
|
|
|
|
2019-04-19 10:09:05 +02:00
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
2020-03-09 10:38:15 +01:00
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
2019-04-19 10:09:05 +02:00
|
|
|
'is_admin' => 1,
|
|
|
|
]);
|
|
|
|
|
2019-09-11 04:01:49 +02:00
|
|
|
$user->fresh();
|
|
|
|
|
|
|
|
$this->assertTrue($user->companies !== null);
|
2019-11-22 22:10:53 +01:00
|
|
|
$this->assertTrue($user->company_users !== null);
|
|
|
|
$this->assertTrue($user->company_users->first() !== null);
|
|
|
|
$this->assertTrue($user->company_user->account !== null);
|
2019-09-11 04:01:49 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
$this->assertEquals($user->email, 'test@example.com');
|
|
|
|
$this->assertTrue(\Hash::check('123456', $user->password));
|
|
|
|
|
2019-04-19 10:09:05 +02:00
|
|
|
$data = [
|
|
|
|
'email' => 'test@example.com',
|
2020-09-06 11:38:10 +02:00
|
|
|
'password' => '123456',
|
2019-04-19 10:09:05 +02:00
|
|
|
];
|
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
try{
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2020-11-01 06:09:09 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-04-19 10:09:05 +02:00
|
|
|
])->post('/api/v1/login', $data);
|
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
info(print_r($message,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
info(print_r($arr,1));
|
|
|
|
|
2019-04-19 10:09:05 +02:00
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
2018-10-24 12:24:09 +02:00
|
|
|
}
|