Fixes for tests (#3701)

* Update Exchange rate Data once a day

* Tests for currency conversions

* Fixes for tests

* Fix for adding blank product keys

* Class for logging emails sent

* Fixes for tests

* Fixes for testS

* Include credits in first_load=true

* Fixes for tests

* fixes for tests

* Fixes for tests:

* Fixes for tests

* Fixes for tests
This commit is contained in:
David Bomba 2020-05-14 19:08:49 +10:00 committed by GitHub
parent 5d54d4313a
commit a0eecdd755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 137 additions and 198 deletions

View File

@ -16,6 +16,6 @@ DB_DATABASE=ninja
DB_USERNAME=root
DB_PASSWORD=ninja
DB_HOST=127.0.0.1
NINJA_ENVIRONMENT=development
NINJA_ENVIRONMENT=hosted
COMPOSER_AUTH='{"github-oauth": {"github.com": "${{ secrets.GITHUB_TOKEN }}"}}'
TRAVIS=true

View File

@ -20,7 +20,7 @@ jobs:
CACHE_DRIVER: file
QUEUE_CONNECTION: sync
SESSION_DRIVER: file
NINJA_ENVIRONMENT: development
NINJA_ENVIRONMENT: hosted
MULTI_DB_ENABLED: false
NINJA_LICENSE: 123456
TRAVIS: true

View File

@ -21,7 +21,7 @@ jobs:
- name: Install composer dependencies
run: |
composer config -g github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}
composer install
composer install --no-dev
- name: Prepare Laravel Application
run: |

View File

@ -264,8 +264,9 @@ class BaseController extends Controller
'company.payments.paymentables',
'company.quotes.invitations.contact',
'company.quotes.invitations.company',
'company.credits.invitations.contact',
'company.credits.invitations.company',
'company.credits',
//'company.credits.invitations.contact',
//'company.credits.invitations.company',
'company.vendors.contacts',
'company.expenses',
'company.tasks',

View File

@ -43,20 +43,26 @@ class InvoiceEmailedNotification implements ShouldQueue
$invitation = $event->invitation;
foreach ($invitation->company->company_users as $company_user) {
$user = $company_user->user;
$notification = new EntitySentNotification($invitation, 'invoice');
$notification->method = $this->findUserNotificationTypes($invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']);
$methods = $this->findUserNotificationTypes($invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']);
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
//Fire mail notification here!!!
//This allows us better control of how we
//handle the mailer
}
$notification->method = $methods;
$user->notify($notification);
}
// if(isset($invitation->company->slack_webhook_url)){
// Notification::route('slack', $invitation->company->slack_webhook_url)
// ->notify(new EntitySentNotification($invitation, $invitation->company, true));
// }
}
}

View File

@ -18,6 +18,7 @@ use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\CompanyLedger;
use App\Models\CompanyUser;
use App\Models\Credit;
use App\Models\Design;
use App\Models\Expense;
use App\Models\GroupSetting;
@ -29,6 +30,7 @@ use App\Models\Task;
use App\Models\TaxRate;
use App\Models\User;
use App\Transformers\CompanyLedgerTransformer;
use App\Transformers\CreditTransformer;
use App\Transformers\TaskTransformer;
use App\Utils\Traits\MakesHash;
@ -68,6 +70,7 @@ class CompanyTransformer extends EntityTransformer
'company_gateways',
'activities',
'quotes',
'credits',
'projects',
'tasks',
'ledger',
@ -202,6 +205,13 @@ class CompanyTransformer extends EntityTransformer
return $this->includeCollection($company->quotes, $transformer, Quote::class);
}
public function includeCredits(Company $company)
{
$transformer = new CreditTransformer($this->serializer);
return $this->includeCollection($company->credits, $transformer, Credit::class);
}
public function includeAccount(Company $company)
{
$transformer = new AccountTransformer($this->serializer);

View File

@ -58,9 +58,6 @@ trait UserNotifies
array_push($notifiable_methods, 'mail');
}
// if(count(array_intersect($required_permissions, $notifications->slack)) >=1)
// array_push($notifiable_methods, 'slack');
return $notifiable_methods;
}

View File

@ -42,6 +42,12 @@ class AccountTest extends TestCase
public function testApiAccountCreation()
{
Account::all()->each(function($account) {
$account->delete();
});
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,

View File

@ -20,8 +20,9 @@ use Illuminate\Http\Request;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
/**
@ -32,6 +33,7 @@ class ClientTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
public function setUp() :void
{
@ -51,38 +53,41 @@ class ClientTest extends TestCase
$this->withoutMiddleware(
ThrottleRequests::class
);
$this->makeTestData();
}
public function testClientList()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
// $data = [
// 'first_name' => $this->faker->firstName,
// 'last_name' => $this->faker->lastName,
// 'name' => $this->faker->company,
// 'email' => $this->faker->unique()->safeEmail,
// 'password' => 'ALongAndBrilliantPassword123',
// '_token' => csrf_token(),
// 'privacy_policy' => 1,
// 'terms_of_service' => 1
// ];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup?include=account', $data);
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// ])->post('/api/v1/signup?include=account', $data);
$response->assertStatus(200);
// $response->assertStatus(200);
$acc = $response->json();
// $acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
// $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
$token = $account->default_company->tokens->first()->token;
// $this->token = $account->default_company->tokens->first()->token;
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->get('/api/v1/clients');
$response->assertStatus(200);
@ -93,72 +98,34 @@ class ClientTest extends TestCase
*/
public function testClientRestEndPoints()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup?include=account', $data);
$acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;
$company = $company_token->company;
$user = $company_token->user;
//$company_user = $company->company_users()->first();
//$user = User::find($company_user->user_id);
$this->assertNotNull($company_token);
$this->assertNotNull($token);
$this->assertNotNull($user);
$this->assertNotNull($company);
//$this->assertNotNull($user->token->company);
factory(\App\Models\Client::class, 3)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) {
factory(\App\Models\Client::class, 3)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c){
factory(\App\Models\ClientContact::class, 1)->create([
'user_id' => $user->id,
'user_id' => $this->user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'company_id' => $this->company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class, 2)->create([
'user_id' => $user->id,
'user_id' => $this->user->id,
'client_id' => $c->id,
'company_id' => $company->id
'company_id' => $this->company->id
]);
});
$client = $account->default_company->clients()->first();
$client->load('contacts');
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($client->id));
'X-API-TOKEN' => $this->token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id));
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($client->id).'/edit');
'X-API-TOKEN' => $this->token,
])->get('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id).'/edit');
$response->assertStatus(200);
@ -168,29 +135,29 @@ class ClientTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($client->id), $client_update)
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id), $client_update)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->delete('/api/v1/clients/'.$this->encodePrimaryKey($client->id));
'X-API-TOKEN' => $this->token,
])->delete('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id));
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', ['name' => 'New Client'])
->assertStatus(200);
$response->assertStatus(200);
$client->is_deleted = true;
$client->save();
$this->client->is_deleted = true;
$this->client->save();
$client_update = [
@ -199,8 +166,8 @@ class ClientTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($client->id), $client_update)
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/'.$this->encodePrimaryKey($this->client->id), $client_update)
->assertStatus(400);
}
@ -216,7 +183,8 @@ class ClientTest extends TestCase
$user = factory(\App\Models\User::class)->create([
'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default'))
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => 'whiz@gmail.com'
]);
@ -256,18 +224,18 @@ class ClientTest extends TestCase
]);
});
$client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
$this->client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
$this->assertNotNull($client);
$this->assertNotNull($this->client);
/* Make sure we have a valid settings object*/
$this->assertEquals($client->getSetting('timezone_id'), 1);
$this->assertEquals($this->client->getSetting('timezone_id'), 1);
/* Make sure we are harvesting valid data */
$this->assertEquals($client->timezone()->name, 'Pacific/Midway');
$this->assertEquals($this->client->timezone()->name, 'Pacific/Midway');
/* Make sure NULL settings return the correct count (0) instead of throwing an exception*/
$this->assertEquals($client->contacts->count(), 3);
$this->assertEquals($this->client->contacts->count(), 3);
}
@ -283,7 +251,9 @@ class ClientTest extends TestCase
$user = factory(\App\Models\User::class)->create([
'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default'))
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => 'whiz@gmail.com'
]);
$user->companies()->attach($company->id, [
@ -305,7 +275,7 @@ class ClientTest extends TestCase
$company_token->save();
$token = $company_token->token;
$this->token = $company_token->token;
$data = [
'name' => 'A loyal Client',
@ -317,7 +287,7 @@ class ClientTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data)
->assertStatus(200);
@ -336,7 +306,7 @@ class ClientTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data)
->assertStatus(200);
@ -356,7 +326,7 @@ class ClientTest extends TestCase
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
@ -378,7 +348,7 @@ class ClientTest extends TestCase
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
@ -405,7 +375,7 @@ class ClientTest extends TestCase
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
@ -417,12 +387,12 @@ class ClientTest extends TestCase
$arr = $response->json();
$client_id = $arr['data']['id'];
$this->client_id = $arr['data']['id'];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/' . $client_id, $data)->assertStatus(200);
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/' . $this->client_id, $data)->assertStatus(200);
$arr = $response->json();
@ -443,7 +413,7 @@ class ClientTest extends TestCase
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
@ -454,9 +424,9 @@ class ClientTest extends TestCase
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$this->client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$contact = $client->contacts()->whereEmail($safe_email)->first();
$contact = $this->client->contacts()->whereEmail($safe_email)->first();
$this->assertEquals(0, strlen($contact->password));
@ -477,7 +447,7 @@ class ClientTest extends TestCase
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post('/api/v1/clients/', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
@ -488,9 +458,9 @@ class ClientTest extends TestCase
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$this->client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$contact = $client->contacts()->whereEmail($safe_email)->first();
$contact = $this->client->contacts()->whereEmail($safe_email)->first();
$this->assertGreaterThan(1, strlen($contact->password));
@ -512,8 +482,8 @@ class ClientTest extends TestCase
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/clients/' . $client->hashed_id, $data);
'X-API-TOKEN' => $this->token,
])->put('/api/v1/clients/' . $this->client->hashed_id, $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
@ -523,58 +493,13 @@ class ClientTest extends TestCase
$arr = $response->json();
$client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$client->fresh();
$this->client = Client::find($this->decodePrimaryKey($arr['data']['id']));
$this->client->fresh();
$contact = $client->contacts()->whereEmail($safe_email)->first();
$contact = $this->client->contacts()->whereEmail($safe_email)->first();
$this->assertEquals($password, $contact->password);
}
/** @test */
// public function testMassivelyCreatingClients()
// {
// $data = [
// 'first_name' => $this->faker->firstName,
// 'last_name' => $this->faker->lastName,
// 'name' => $this->faker->company,
// 'email' => $this->faker->unique()->safeEmail,
// 'password' => 'ALongAndBrilliantPassword123',
// '_token' => csrf_token(),
// 'privacy_policy' => 1,
// 'terms_of_service' => 1
// ];
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// ])->post('/api/v1/signup?include=account', $data);
// $response->assertStatus(200);
// $acc = $response->json();
// $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
// $token = $account->default_company->tokens->first()->token;
// $body = [
// 'action' => 'create',
// 'clients' => [
// ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-1.com'],
// ['name' => $this->faker->firstName, 'website' => 'my-awesome-website-2.com'],
// ],
// ];
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $token,
// ])->post(route('clients.bulk'), $body);
// $response->assertStatus(200);
// $first_record = Client::where('website', 'my-awesome-website-1.com')->first();
// $second_record = Client::where('website', 'my-awesome-website-2.com')->first();
// $this->assertNotNull($first_record);
// $this->assertNotNull($second_record);
// }
}

View File

@ -19,6 +19,7 @@ use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
/**
@ -28,6 +29,7 @@ use Tests\TestCase;
class CompanyTest extends TestCase
{
use MakesHash;
use MockAccountData;
use DatabaseTransactions;
@ -40,38 +42,17 @@ class CompanyTest extends TestCase
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
}
public function testCompanyList()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup?include=account', $data);
$response->assertStatus(200);
$acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
$token = $account->default_company->tokens->first()->token;
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->get('/api/v1/companies');
$response->assertStatus(200);
@ -79,7 +60,7 @@ class CompanyTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post(
'/api/v1/companies?include=company',
[
@ -93,7 +74,7 @@ class CompanyTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->post(
'/api/v1/companies/',
[
@ -105,7 +86,7 @@ class CompanyTest extends TestCase
// Log::error($company);
$token = CompanyToken::whereCompanyId($company->id)->first()->token;
$this->token = CompanyToken::whereCompanyId($company->id)->first()->token;
$company_update = [
'name' => 'CHANGE NAME',
@ -114,7 +95,7 @@ class CompanyTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->put('/api/v1/companies/'.$this->encodePrimaryKey($company->id), $company_update)
->assertStatus(200);
@ -128,19 +109,19 @@ class CompanyTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->put('/api/v1/companies/'.$this->encodePrimaryKey($company->id), $company->toArray())
->assertStatus(200)->decodeResponseJson();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->get('/api/v1/companies/'.$this->encodePrimaryKey($company->id))
->assertStatus(200)->decodeResponseJson();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
'X-API-TOKEN' => $this->token,
])->delete('/api/v1/companies/'.$this->encodePrimaryKey($company->id))
->assertStatus(200);
}

View File

@ -33,6 +33,11 @@ class CreditTest extends TestCase
public function testCreditsList()
{
Account::all()->each(function($account) {
$account->delete();
});
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,

View File

@ -44,6 +44,10 @@ class InvoiceTest extends TestCase
public function testInvoiceList()
{
Account::all()->each(function($account) {
$account->delete();
});
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,

View File

@ -56,13 +56,11 @@ class PreviewTest extends TestCase
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->post('/api/v1/preview', $data);
$response->assertStatus(200);
])->post('/api/v1/preview/', $data)->assertStatus(200);
}

View File

@ -44,6 +44,12 @@ class ProductTest extends TestCase
public function testProductList()
{
Account::all()->each(function($account) {
$account->delete();
});
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,