mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
118 lines
4.1 KiB
PHP
118 lines
4.1 KiB
PHP
<?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 Log;
|
|
use Mockery;
|
|
use Preferences;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Class DoAuthenticateHandlerTest
|
|
*/
|
|
class DoAuthenticateHandlerTest extends TestCase
|
|
{
|
|
/**
|
|
*
|
|
*/
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Log::info(sprintf('Now in %s.', \get_class($this)));
|
|
}
|
|
|
|
/**
|
|
* No token in config, but grab it from users preferences.
|
|
*
|
|
* @covers \FireflyIII\Support\Import\Information\GetSpectreTokenTrait
|
|
* @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, 10000);
|
|
$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);
|
|
}
|
|
|
|
}
|