mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Improve test coverage.
This commit is contained in:
@@ -0,0 +1,652 @@
|
||||
<?php
|
||||
/**
|
||||
* ChooseAccountsHandlerTest.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Support\Import\JobConfiguration\Spectre;
|
||||
|
||||
|
||||
use Amount;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Services\Spectre\Object\Account as SpectreAccount;
|
||||
use FireflyIII\Services\Spectre\Object\Attempt;
|
||||
use FireflyIII\Services\Spectre\Object\Holder;
|
||||
use FireflyIII\Services\Spectre\Object\Login;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler;
|
||||
use Illuminate\Support\Collection;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class ChooseAccountsHandlerTest
|
||||
*/
|
||||
class ChooseAccountsHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testCCFalse(): void
|
||||
{
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-A' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'account_mapping' => [],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$this->assertFalse($handler->configurationComplete());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testCCTrue(): void
|
||||
{
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-B' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'account_mapping' => [
|
||||
4 => 6,
|
||||
],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setStage')->withArgs([Mockery::any(), 'go-for-import'])->once();
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$this->assertTrue($handler->configurationComplete());
|
||||
}
|
||||
|
||||
/**
|
||||
* Case: Local account is valid. Spectre account is valid.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testConfigureJob(): void
|
||||
{
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-c' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'accounts' => [
|
||||
0 => [
|
||||
'id' => 3131,
|
||||
'name' => 'Some fake account',
|
||||
],
|
||||
],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
$account = $this->user()->accounts()->inRandomOrder()->first();
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'account_mapping' => [3131 => 872,],
|
||||
];
|
||||
// expected configuration:
|
||||
$config = [
|
||||
'accounts' => [0 => ['id' => 3131, 'name' => 'Some fake account',],],
|
||||
'account_mapping' => [3131 => 872,],
|
||||
];
|
||||
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
$accountRepos->shouldReceive('findNull')->once()->withArgs([872])->andReturn($account);
|
||||
$importRepos->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $config]);
|
||||
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$this->assertCount(0, $handler->configureJob($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Case: Local account is invalid. Spectre account is valid.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testConfigureJobInvalidLocal(): void
|
||||
{
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-D' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'accounts' => [
|
||||
0 => [
|
||||
'id' => 3131,
|
||||
'name' => 'Some fake account',
|
||||
],
|
||||
],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'account_mapping' => [3131 => 872,],
|
||||
];
|
||||
// expected configuration:
|
||||
$config = [
|
||||
'accounts' => [0 => ['id' => 3131, 'name' => 'Some fake account',],],
|
||||
'account_mapping' => [3131 => 0,],
|
||||
];
|
||||
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
$accountRepos->shouldReceive('findNull')->once()->withArgs([872])->andReturn(null);
|
||||
$importRepos->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $config]);
|
||||
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$this->assertCount(0, $handler->configureJob($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Case: Local account is valid. Spectre account is invalid.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testConfigureJobInvalidSpectre(): void
|
||||
{
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-E' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'accounts' => [
|
||||
0 => [
|
||||
'id' => 3134,
|
||||
'name' => 'Some fake account',
|
||||
],
|
||||
],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
$account = $this->user()->accounts()->inRandomOrder()->first();
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'account_mapping' => [3131 => 872,],
|
||||
];
|
||||
// expected configuration:
|
||||
$config = [
|
||||
'accounts' => [0 => ['id' => 3134, 'name' => 'Some fake account',],],
|
||||
'account_mapping' => [0 => 872,],
|
||||
];
|
||||
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
$accountRepos->shouldReceive('findNull')->once()->withArgs([872])->andReturn($account);
|
||||
$importRepos->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $config]);
|
||||
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$this->assertCount(0, $handler->configureJob($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Case: Local account is invalid. Spectre account is invalid.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testConfigureJobInvalidBoth(): void
|
||||
{
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-E' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'accounts' => [
|
||||
0 => [
|
||||
'id' => 3134,
|
||||
'name' => 'Some fake account',
|
||||
],
|
||||
],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'account_mapping' => [3131 => 872,],
|
||||
];
|
||||
// expected configuration:
|
||||
$config = [
|
||||
'accounts' => [0 => ['id' => 3134, 'name' => 'Some fake account',],],
|
||||
'account_mapping' => [0 => 0,],
|
||||
];
|
||||
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
$accountRepos->shouldReceive('findNull')->once()->withArgs([872])->andReturn(null);
|
||||
$importRepos->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $config]);
|
||||
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$result =$handler->configureJob($data);
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('It seems you have not selected any accounts to import from.', $result->first());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testGetNextData(): void
|
||||
{
|
||||
// needs to be a full spectre account this time.
|
||||
$spectreAccount = [
|
||||
'id' => 1234,
|
||||
'login_id' => 5678,
|
||||
'currency_code' => 'EUR',
|
||||
'balance' => 1000,
|
||||
'name' => 'Fake Spectre Account',
|
||||
'nature' => 'account',
|
||||
'created_at' => '2018-01-01 12:12:12',
|
||||
'updated_at' => '2018-01-01 12:12:12',
|
||||
'extra' => [],
|
||||
];
|
||||
|
||||
|
||||
// need to be a full spectre login this time.
|
||||
$holder = new Holder([]);
|
||||
$attempt = new Attempt(
|
||||
[
|
||||
'api_mode' => 'x',
|
||||
'api_version' => 4,
|
||||
'automatic_fetch' => true,
|
||||
'categorize' => true,
|
||||
'created_at' => '2018-05-21 12:00:00',
|
||||
'consent_given_at' => '2018-05-21 12:00:00',
|
||||
'consent_types' => ['transactions'],
|
||||
'custom_fields' => [],
|
||||
'daily_refresh' => true,
|
||||
'device_type' => 'mobile',
|
||||
'user_agent' => 'Mozilla/x',
|
||||
'remote_ip' => '127.0.0.1',
|
||||
'exclude_accounts' => [],
|
||||
'fail_at' => '2018-05-21 12:00:00',
|
||||
'fail_error_class' => 'err',
|
||||
'fail_message' => 'message',
|
||||
'fetch_scopes' => [],
|
||||
'finished' => true,
|
||||
'finished_recent' => true,
|
||||
'from_date' => '2018-05-21 12:00:00',
|
||||
'id' => 1,
|
||||
'interactive' => true,
|
||||
'locale' => 'en',
|
||||
'partial' => true,
|
||||
'show_consent_confirmation' => true,
|
||||
'stages' => [],
|
||||
'store_credentials' => true,
|
||||
'success_at' => '2018-05-21 12:00:00',
|
||||
'to_date' => '2018-05-21 12:00:00',
|
||||
'updated_at' => '2018-05-21 12:00:00',
|
||||
]
|
||||
);
|
||||
$spectreLogin = new Login(
|
||||
[
|
||||
'consent_given_at' => '2018-05-21 12:00:00',
|
||||
'consent_types' => ['transactions'],
|
||||
'country_code' => 'NL',
|
||||
'created_at' => '2018-05-21 12:00:00',
|
||||
'updated_at' => '2018-05-21 12:00:00',
|
||||
'customer_id' => '1',
|
||||
'daily_refresh' => true,
|
||||
'holder_info' => $holder->toArray(),
|
||||
'id' => 1234,
|
||||
'last_attempt' => $attempt->toArray(),
|
||||
'last_success_at' => '2018-05-21 12:00:00',
|
||||
'next_refresh_possible_at' => '2018-05-21 12:00:00',
|
||||
'provider_code' => 'XF',
|
||||
'provider_id' => '123',
|
||||
'provider_name' => 'Fake',
|
||||
'show_consent_confirmation' => true,
|
||||
'status' => 'active',
|
||||
'store_credentials' => true,
|
||||
]
|
||||
);
|
||||
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-F' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'accounts' => [
|
||||
0 => $spectreAccount,
|
||||
],
|
||||
'all-logins' => [
|
||||
0 => $spectreLogin->toArray(),
|
||||
],
|
||||
'selected-login' => 1234,
|
||||
];
|
||||
$job->save();
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
|
||||
$euro = TransactionCurrency::where('code', 'EUR')->first();
|
||||
$usd = TransactionCurrency::where('code', 'USD')->first();
|
||||
$first = $this->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$second = $this->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $first->id)->first();
|
||||
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET]])
|
||||
->once()->andReturn(new Collection([$first, $second]));
|
||||
$accountRepos->shouldReceive('getMetaValue')->twice()->withArgs([Mockery::any(), 'currency_id'])
|
||||
->andReturn(1, 2);
|
||||
$currencyRepos->shouldReceive('findNull')->once()->withArgs([1])->andReturn($euro);
|
||||
$currencyRepos->shouldReceive('findNull')->once()->withArgs([2])->andReturn(null);
|
||||
Amount::shouldReceive('getDefaultCurrencyByUser')->withArgs([Mockery::any()])->once()->andReturn($usd);
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$result = [];
|
||||
try {
|
||||
$result = $handler->getNextData();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
|
||||
$expected = [
|
||||
'accounts' => [
|
||||
0 => new SpectreAccount($spectreAccount),
|
||||
],
|
||||
'ff_accounts' => [
|
||||
$first->id => [
|
||||
'name' => $first->name,
|
||||
'iban' => $first->iban,
|
||||
'code' => $euro->code,
|
||||
],
|
||||
$second->id => [
|
||||
'name' => $second->name,
|
||||
'iban' => $second->iban,
|
||||
'code' => $usd->code,
|
||||
],
|
||||
],
|
||||
'login' => $spectreLogin,
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select first login.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccountsHandler
|
||||
*/
|
||||
public function testGetNextDataZero(): void
|
||||
{
|
||||
// needs to be a full spectre account this time.
|
||||
$spectreAccount = [
|
||||
'id' => 1234,
|
||||
'login_id' => 5678,
|
||||
'currency_code' => 'EUR',
|
||||
'balance' => 1000,
|
||||
'name' => 'Fake Spectre Account',
|
||||
'nature' => 'account',
|
||||
'created_at' => '2018-01-01 12:12:12',
|
||||
'updated_at' => '2018-01-01 12:12:12',
|
||||
'extra' => [],
|
||||
];
|
||||
|
||||
|
||||
// need to be a full spectre login this time.
|
||||
$holder = new Holder([]);
|
||||
$attempt = new Attempt(
|
||||
[
|
||||
'api_mode' => 'x',
|
||||
'api_version' => 4,
|
||||
'automatic_fetch' => true,
|
||||
'categorize' => true,
|
||||
'created_at' => '2018-05-21 12:00:00',
|
||||
'consent_given_at' => '2018-05-21 12:00:00',
|
||||
'consent_types' => ['transactions'],
|
||||
'custom_fields' => [],
|
||||
'daily_refresh' => true,
|
||||
'device_type' => 'mobile',
|
||||
'user_agent' => 'Mozilla/x',
|
||||
'remote_ip' => '127.0.0.1',
|
||||
'exclude_accounts' => [],
|
||||
'fail_at' => '2018-05-21 12:00:00',
|
||||
'fail_error_class' => 'err',
|
||||
'fail_message' => 'message',
|
||||
'fetch_scopes' => [],
|
||||
'finished' => true,
|
||||
'finished_recent' => true,
|
||||
'from_date' => '2018-05-21 12:00:00',
|
||||
'id' => 1,
|
||||
'interactive' => true,
|
||||
'locale' => 'en',
|
||||
'partial' => true,
|
||||
'show_consent_confirmation' => true,
|
||||
'stages' => [],
|
||||
'store_credentials' => true,
|
||||
'success_at' => '2018-05-21 12:00:00',
|
||||
'to_date' => '2018-05-21 12:00:00',
|
||||
'updated_at' => '2018-05-21 12:00:00',
|
||||
]
|
||||
);
|
||||
$spectreLogin = new Login(
|
||||
[
|
||||
'consent_given_at' => '2018-05-21 12:00:00',
|
||||
'consent_types' => ['transactions'],
|
||||
'country_code' => 'NL',
|
||||
'created_at' => '2018-05-21 12:00:00',
|
||||
'updated_at' => '2018-05-21 12:00:00',
|
||||
'customer_id' => '1',
|
||||
'daily_refresh' => true,
|
||||
'holder_info' => $holder->toArray(),
|
||||
'id' => 1234,
|
||||
'last_attempt' => $attempt->toArray(),
|
||||
'last_success_at' => '2018-05-21 12:00:00',
|
||||
'next_refresh_possible_at' => '2018-05-21 12:00:00',
|
||||
'provider_code' => 'XF',
|
||||
'provider_id' => '123',
|
||||
'provider_name' => 'Fake',
|
||||
'show_consent_confirmation' => true,
|
||||
'status' => 'active',
|
||||
'store_credentials' => true,
|
||||
]
|
||||
);
|
||||
|
||||
// fake job:
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sca-F' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'accounts' => [
|
||||
0 => $spectreAccount,
|
||||
],
|
||||
'all-logins' => [
|
||||
0 => $spectreLogin->toArray(),
|
||||
],
|
||||
'selected-login' => 0,
|
||||
];
|
||||
$job->save();
|
||||
|
||||
// mock repositories:
|
||||
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$importRepos = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$accountRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$importRepos->shouldReceive('setUser')->once();
|
||||
|
||||
$euro = TransactionCurrency::where('code', 'EUR')->first();
|
||||
$usd = TransactionCurrency::where('code', 'USD')->first();
|
||||
$first = $this->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$second = $this->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $first->id)->first();
|
||||
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET]])
|
||||
->once()->andReturn(new Collection([$first, $second]));
|
||||
$accountRepos->shouldReceive('getMetaValue')->twice()->withArgs([Mockery::any(), 'currency_id'])
|
||||
->andReturn(1, 2);
|
||||
$currencyRepos->shouldReceive('findNull')->once()->withArgs([1])->andReturn($euro);
|
||||
$currencyRepos->shouldReceive('findNull')->once()->withArgs([2])->andReturn(null);
|
||||
Amount::shouldReceive('getDefaultCurrencyByUser')->withArgs([Mockery::any()])->once()->andReturn($usd);
|
||||
|
||||
// call handler:
|
||||
$handler = new ChooseAccountsHandler();
|
||||
$handler->setImportJob($job);
|
||||
$result = [];
|
||||
try {
|
||||
$result = $handler->getNextData();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
|
||||
$expected = [
|
||||
'accounts' => [
|
||||
0 => new SpectreAccount($spectreAccount),
|
||||
],
|
||||
'ff_accounts' => [
|
||||
$first->id => [
|
||||
'name' => $first->name,
|
||||
'iban' => $first->iban,
|
||||
'code' => $euro->code,
|
||||
],
|
||||
$second->id => [
|
||||
'name' => $second->name,
|
||||
'iban' => $second->iban,
|
||||
'code' => $usd->code,
|
||||
],
|
||||
],
|
||||
'login' => $spectreLogin,
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
/**
|
||||
* ChooseLoginHandlerTest.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Support\Import\JobConfiguration\Spectre;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Services\Spectre\Object\Attempt;
|
||||
use FireflyIII\Services\Spectre\Object\Holder;
|
||||
use FireflyIII\Services\Spectre\Object\Login;
|
||||
use FireflyIII\Services\Spectre\Object\Token;
|
||||
use FireflyIII\Services\Spectre\Request\CreateTokenRequest;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler;
|
||||
use Mockery;
|
||||
use Preferences;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class ChooseLoginHandlerTest
|
||||
*/
|
||||
class ChooseLoginHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler
|
||||
*/
|
||||
public function testCCFalse(): void
|
||||
{
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'slh-A' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [];
|
||||
$job->save();
|
||||
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
$handler = new ChooseLoginHandler;
|
||||
$handler->setImportJob($job);
|
||||
$this->assertFalse($handler->configurationComplete());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler
|
||||
*/
|
||||
public function testCCTrue(): void
|
||||
{
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'slh-B' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = ['selected-login' => 1,];
|
||||
$job->save();
|
||||
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
$handler = new ChooseLoginHandler;
|
||||
$handler->setImportJob($job);
|
||||
$this->assertTrue($handler->configurationComplete());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler
|
||||
*/
|
||||
public function testConfigureJob(): void
|
||||
{
|
||||
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'slh-C' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [];
|
||||
$job->save();
|
||||
$data = [
|
||||
'spectre_login_id' => 12,
|
||||
];
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('setConfiguration')->withArgs([Mockery::any(), ['selected-login' => 12],])->once();
|
||||
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'authenticated']);
|
||||
|
||||
$handler = new ChooseLoginHandler;
|
||||
$handler->setImportJob($job);
|
||||
try {
|
||||
$this->assertCount(0, $handler->configureJob($data));
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler
|
||||
*/
|
||||
public function testConfigureJobCustomer(): void
|
||||
{
|
||||
// fake Spectre customer:
|
||||
$fakeCustomerPreference = new Preference;
|
||||
$fakeCustomerPreference->name = 'spectre_customer';
|
||||
$fakeCustomerPreference->data = [
|
||||
'id' => 1,
|
||||
'identifier' => 'fake',
|
||||
'secret' => 'Dumbledore dies',
|
||||
];
|
||||
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'slh-C' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [];
|
||||
$job->save();
|
||||
$data = [
|
||||
'spectre_login_id' => 0,
|
||||
];
|
||||
$carbon = new Carbon();
|
||||
$token = new Token(['token' => 'x', 'expires_at' => $carbon->toW3cString(), 'connect_url' => 'https://']);
|
||||
|
||||
// should try to grab customer from preferences:
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'spectre_customer', null])
|
||||
->andReturn($fakeCustomerPreference)->once();
|
||||
|
||||
// mock stuff
|
||||
$ctRequest = $this->mock(CreateTokenRequest::class);
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
|
||||
// mock calls
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), ['selected-login' => 0,]]);
|
||||
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'do-authenticate']);
|
||||
$repository->shouldReceive('setConfiguration')->once()->withArgs(
|
||||
[Mockery::any(),
|
||||
[
|
||||
'selected-login' => 0,
|
||||
'customer' => ['id' => 1, 'identifier' => 'fake', 'secret' => 'Dumbledore dies',],
|
||||
'token' => ['token' => 'x', 'expires_at' => $carbon->toW3cString(), 'connect_url' => 'https://'],
|
||||
]]
|
||||
);
|
||||
|
||||
// should try to grab token from Spectre:
|
||||
$ctRequest->shouldReceive('setUser')->once();
|
||||
$ctRequest->shouldReceive('setCustomer')->once();
|
||||
$ctRequest->shouldReceive('setUri')->once()->withArgs([route('import.job.status.index', [$job->key])]);
|
||||
$ctRequest->shouldReceive('call')->once();
|
||||
$ctRequest->shouldReceive('getToken')->once()->andReturn($token);
|
||||
|
||||
|
||||
$handler = new ChooseLoginHandler;
|
||||
$handler->setImportJob($job);
|
||||
try {
|
||||
$this->assertCount(0, $handler->configureJob($data));
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler
|
||||
*/
|
||||
public function testGetNextData(): void
|
||||
{
|
||||
// fake login:
|
||||
$holder = new Holder([]);
|
||||
$attempt = new Attempt(
|
||||
[
|
||||
'api_mode' => 'x',
|
||||
'api_version' => 4,
|
||||
'automatic_fetch' => true,
|
||||
'categorize' => true,
|
||||
'created_at' => '2018-05-21 12:00:00',
|
||||
'consent_given_at' => '2018-05-21 12:00:00',
|
||||
'consent_types' => ['transactions'],
|
||||
'custom_fields' => [],
|
||||
'daily_refresh' => true,
|
||||
'device_type' => 'mobile',
|
||||
'user_agent' => 'Mozilla/x',
|
||||
'remote_ip' => '127.0.0.1',
|
||||
'exclude_accounts' => [],
|
||||
'fail_at' => '2018-05-21 12:00:00',
|
||||
'fail_error_class' => 'err',
|
||||
'fail_message' => 'message',
|
||||
'fetch_scopes' => [],
|
||||
'finished' => true,
|
||||
'finished_recent' => true,
|
||||
'from_date' => '2018-05-21 12:00:00',
|
||||
'id' => 1,
|
||||
'interactive' => true,
|
||||
'locale' => 'en',
|
||||
'partial' => true,
|
||||
'show_consent_confirmation' => true,
|
||||
'stages' => [],
|
||||
'store_credentials' => true,
|
||||
'success_at' => '2018-05-21 12:00:00',
|
||||
'to_date' => '2018-05-21 12:00:00',
|
||||
'updated_at' => '2018-05-21 12:00:00',
|
||||
]
|
||||
);
|
||||
$login = new Login(
|
||||
[
|
||||
'consent_given_at' => '2018-05-21 12:00:00',
|
||||
'consent_types' => ['transactions'],
|
||||
'country_code' => 'NL',
|
||||
'created_at' => '2018-05-21 12:00:00',
|
||||
'updated_at' => '2018-05-21 12:00:00',
|
||||
'customer_id' => '1',
|
||||
'daily_refresh' => true,
|
||||
'holder_info' => $holder->toArray(),
|
||||
'id' => 123,
|
||||
'last_attempt' => $attempt->toArray(),
|
||||
'last_success_at' => '2018-05-21 12:00:00',
|
||||
'next_refresh_possible_at' => '2018-05-21 12:00:00',
|
||||
'provider_code' => 'XF',
|
||||
'provider_id' => '123',
|
||||
'provider_name' => 'Fake',
|
||||
'show_consent_confirmation' => true,
|
||||
'status' => 'active',
|
||||
'store_credentials' => true,
|
||||
]
|
||||
);
|
||||
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'slh-C' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [
|
||||
'all-logins' => [$login->toArray()],
|
||||
];
|
||||
$job->save();
|
||||
|
||||
$handler = new ChooseLoginHandler;
|
||||
$handler->setImportJob($job);
|
||||
$this->assertEquals(['logins' => [$login]], $handler->getNextData());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* DoAuthenticateHandlerTest.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests\Unit\Support\Import\JobConfiguration\Spectre;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Services\Spectre\Object\Token;
|
||||
use FireflyIII\Services\Spectre\Request\CreateTokenRequest;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\DoAuthenticateHandler;
|
||||
use Mockery;
|
||||
use Preferences;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class DoAuthenticateHandlerTest
|
||||
*/
|
||||
class DoAuthenticateHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* No token in config, but grab it from users preferences.
|
||||
*
|
||||
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\DoAuthenticateHandler
|
||||
*/
|
||||
public function testGetNextDataNoToken(): void
|
||||
{
|
||||
$job = new ImportJob;
|
||||
$job->user_id = $this->user()->id;
|
||||
$job->key = 'sda-A' . random_int(1, 1000);
|
||||
$job->status = 'new';
|
||||
$job->stage = 'new';
|
||||
$job->provider = 'spectre';
|
||||
$job->file_type = '';
|
||||
$job->configuration = [];
|
||||
$job->save();
|
||||
|
||||
// mock stuff:
|
||||
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'ready_to_run']);
|
||||
$repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'authenticated']);
|
||||
|
||||
// mock request for a new Token:
|
||||
$ctRequest = $this->mock(CreateTokenRequest::class);
|
||||
|
||||
// fake token:
|
||||
$carbon = new Carbon();
|
||||
$token = new Token(['token' => 'x', 'expires_at' => $carbon->toW3cString(), 'connect_url' => 'https://']);
|
||||
|
||||
// fake Spectre customer:
|
||||
$fakeCustomerPreference = new Preference;
|
||||
$fakeCustomerPreference->name = 'spectre_customer';
|
||||
$fakeCustomerPreference->data = [
|
||||
'id' => 1,
|
||||
'identifier' => 'fake',
|
||||
'secret' => 'Dumbledore dies',
|
||||
];
|
||||
|
||||
// should try to grab customer from preferences:
|
||||
Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'spectre_customer', null])
|
||||
->andReturn($fakeCustomerPreference)->once();
|
||||
|
||||
// should try to grab token from Spectre:
|
||||
$ctRequest->shouldReceive('setUser')->once();
|
||||
$ctRequest->shouldReceive('setCustomer')->once();
|
||||
$ctRequest->shouldReceive('setUri')->once()->withArgs([route('import.job.status.index', [$job->key])]);
|
||||
$ctRequest->shouldReceive('call')->once();
|
||||
$ctRequest->shouldReceive('getToken')->once()->andReturn($token);
|
||||
|
||||
$handler = new DoAuthenticateHandler;
|
||||
$handler->setImportJob($job);
|
||||
$result = [];
|
||||
try {
|
||||
$result = $handler->getNextData();
|
||||
} catch (FireflyException $e) {
|
||||
$this->assertFalse(true, $e->getMessage());
|
||||
}
|
||||
$this->assertEquals(['token-url' => $token->getConnectUrl()], $result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user