Files
firefly-iii/tests/Unit/Support/Import/JobConfiguration/Spectre/DoAuthenticateHandlerTest.php

117 lines
4.1 KiB
PHP
Raw Normal View History

2018-05-21 17:28:09 +02:00
<?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);
2018-05-25 23:13:08 +02:00
namespace Tests\Unit\Support\Import\JobConfiguration\Spectre;
2018-05-21 17:28:09 +02:00
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;
use Log;
2018-05-21 17:28:09 +02:00
/**
* Class DoAuthenticateHandlerTest
*/
class DoAuthenticateHandlerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', \get_class($this)));
}
2018-05-21 17:28:09 +02:00
/**
* No token in config, but grab it from users preferences.
2018-08-06 19:14:30 +02:00
*
2018-05-21 19:17:33 +02:00
* @covers \FireflyIII\Support\Import\Information\GetSpectreTokenTrait
2018-05-21 17:28:09 +02:00
* @covers \FireflyIII\Support\Import\JobConfiguration\Spectre\DoAuthenticateHandler
*/
public function testGetNextDataNoToken(): void
{
2018-05-21 19:17:33 +02:00
2018-05-21 17:28:09 +02:00
$job = new ImportJob;
$job->user_id = $this->user()->id;
$job->key = 'sda-A' . random_int(1, 10000);
2018-05-21 17:28:09 +02:00
$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);
}
2018-07-22 20:33:17 +02:00
}