1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 00:53:10 +01:00
invoiceninja/tests/Feature/LoginTest.php

203 lines
5.7 KiB
PHP
Raw Normal View History

<?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) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature;
use App\DataMapper\CompanySettings;
use App\Models\Account;
2020-10-01 13:34:05 +02:00
use App\Models\Company;
2019-09-11 04:01:49 +02:00
use App\Models\CompanyToken;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
2020-11-25 15:19:52 +01:00
use Tests\TestCase;
2019-04-20 01:02:49 +02:00
/**
* @test
* @covers App\Http\Controllers\Auth\LoginController
*/
class LoginTest extends TestCase
{
2019-04-24 02:22:02 +02:00
use DatabaseTransactions;
2019-04-24 12:01:40 +02:00
public function setUp() :void
{
parent::setUp();
Session::start();
}
public function testLoginFormDisplayed()
{
$response = $this->get('/login', [
'_token' => csrf_token(),
]);
$response->assertStatus(404);
}
/**
* A valid user can be logged in.
*
* @return void
*/
// 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([
// '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);
// }
/**
* An invalid user cannot be logged in.
*
* @return void
*/
// 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([
// '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([
// '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);
// // $this->assertGuest();
// }
2019-04-19 10:09:05 +02:00
public function testApiLogin()
{
2020-11-25 15:19:52 +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([
'account_id' => $account->id,
2019-04-19 10:09:05 +02:00
'email' => 'test@example.com',
'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,
]);
$account->default_company_id = $company->id;
2019-09-11 04:01:49 +02:00
$account->save();
$company_token = new CompanyToken;
$company_token->user_id = $user->id;
$company_token->company_id = $company->id;
$company_token->account_id = $account->id;
$company_token->name = $user->first_name.' '.$user->last_name;
$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,
'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);
$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
$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',
'password' => '123456',
2019-04-19 10:09:05 +02:00
];
2020-11-25 15:19:52 +01:00
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
2019-04-19 10:09:05 +02:00
])->post('/api/v1/login', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
2020-12-29 22:10:03 +01:00
nlog(print_r($message, 1));
}
$arr = $response->json();
2021-01-14 10:31:27 +01:00
// nlog(print_r($arr, 1));
2019-04-19 10:09:05 +02:00
$response->assertStatus(200);
}
}