Improve coverage for Spectre.

This commit is contained in:
James Cole
2018-05-21 19:17:33 +02:00
parent 94e6816bf6
commit b195a61498
3 changed files with 462 additions and 60 deletions

View File

@@ -0,0 +1,450 @@
<?php
/**
* StageAuthenticatedHandlerTest.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\Routine\Spectre;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Preference;
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\Services\Spectre\Request\ListAccountsRequest;
use FireflyIII\Services\Spectre\Request\ListLoginsRequest;
use FireflyIII\Support\Import\Routine\Spectre\StageAuthenticatedHandler;
use Mockery;
use Preferences;
use Tests\TestCase;
/**
* Class StageAuthenticatedHandlerTest
*/
class StageAuthenticatedHandlerTest extends TestCase
{
/**
* Already have logins in configuration.
*
* @covers \FireflyIII\Support\Import\Routine\Spectre\StageAuthenticatedHandler
*/
public function testRunBasicHaveLogins(): 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' => 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,
]
);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'sa_a_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'spectre';
$job->file_type = '';
$job->configuration = [
'all-logins' => [
$login->toArray(),
],
'selected-login' => 1234,
];
$job->save();
// needs to be a full spectre account this time.
$spectreAccount = new 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' => [],
]
);
// mock repository:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$laRequest = $this->mock(ListAccountsRequest::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$laRequest->shouldReceive('setUser')->once();
$laRequest->shouldReceive('setLogin')->withArgs([Mockery::any()])->once();
$laRequest->shouldReceive('call')->once();
$laRequest->shouldReceive('getAccounts')->once()->andReturn([$spectreAccount]);
$expected = [
'all-logins' => [
$login->toArray(),
],
'selected-login' => 1234,
'accounts' => [
0 => $spectreAccount->toArray(),
],
];
$repository->shouldReceive('setConfiguration')
->withArgs([Mockery::any(), $expected])->once();
$handler = new StageAuthenticatedHandler;
$handler->setImportJob($job);
try {
$handler->run();
} catch (FireflyException $e) {
$this->assertFalse(true, $e->getMessage());
}
}
/**
* Already have logins in configuration, but none selected.
*
* @covers \FireflyIII\Support\Import\Routine\Spectre\StageAuthenticatedHandler
*/
public function testRunBasicHaveLoginsNull(): 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' => 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,
]
);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'sa_a_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'spectre';
$job->file_type = '';
$job->configuration = [
'all-logins' => [
$login->toArray(),
],
'selected-login' => 0,
];
$job->save();
// needs to be a full spectre account this time.
$spectreAccount = new 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' => [],
]
);
// mock repository:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$laRequest = $this->mock(ListAccountsRequest::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$laRequest->shouldReceive('setUser')->once();
$laRequest->shouldReceive('setLogin')->withArgs([Mockery::any()])->once();
$laRequest->shouldReceive('call')->once();
$laRequest->shouldReceive('getAccounts')->once()->andReturn([$spectreAccount]);
$expected = [
'all-logins' => [
$login->toArray(),
],
'selected-login' => 0,
'accounts' => [
0 => $spectreAccount->toArray(),
],
];
$repository->shouldReceive('setConfiguration')
->withArgs([Mockery::any(), $expected])->once();
$handler = new StageAuthenticatedHandler;
$handler->setImportJob($job);
try {
$handler->run();
} catch (FireflyException $e) {
$this->assertFalse(true, $e->getMessage());
}
}
/**
* No logins in config, will grab them from Spectre.
*
* @covers \FireflyIII\Support\Import\Routine\Spectre\StageAuthenticatedHandler
*/
public function testRunBasicZeroLogins(): 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' => 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,
]
);
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'sa_a_' . random_int(1, 1000);
$job->status = 'new';
$job->stage = 'new';
$job->provider = 'spectre';
$job->file_type = '';
$job->configuration = [
'all-logins' => [],
'selected-login' => 0,
];
$job->save();
// needs to be a full spectre account this time.
$spectreAccount = new 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' => [],
]
);
// mock preference
$fakeCustomerPreference = new Preference;
$fakeCustomerPreference->name = 'spectre_customer';
$fakeCustomerPreference->data = [
'id' => 1,
'identifier' => 'fake',
'secret' => 'Dumbledore dies',
];
// mock call for preferences
Preferences::shouldReceive('getForUser')->once()->withArgs([Mockery::any(), 'spectre_customer', null])->andReturn($fakeCustomerPreference);
// mock repository:
$repository = $this->mock(ImportJobRepositoryInterface::class);
$laRequest = $this->mock(ListAccountsRequest::class);
$llRequest = $this->mock(ListLoginsRequest::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$laRequest->shouldReceive('setUser')->once();
$laRequest->shouldReceive('setLogin')->withArgs([Mockery::any()])->once();
$laRequest->shouldReceive('call')->once();
$laRequest->shouldReceive('getAccounts')->once()->andReturn([$spectreAccount]);
// mock calls for list logins (return empty list for now).
$llRequest->shouldReceive('setUser')->once();
$llRequest->shouldReceive('setCustomer')->once();
$llRequest->shouldReceive('call')->once();
$llRequest->shouldReceive('getLogins')->once()->andReturn([$login]);
$expected = [
'all-logins' => [
$login->toArray(),
],
'selected-login' => 0,
'accounts' => [
0 => $spectreAccount->toArray(),
],
];
$repository->shouldReceive('setConfiguration')
->withArgs([Mockery::any(), $expected])->once();
$handler = new StageAuthenticatedHandler;
$handler->setImportJob($job);
try {
$handler->run();
} catch (FireflyException $e) {
$this->assertFalse(true, $e->getMessage());
}
}
}