mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 12:45:30 +00:00
Expand API validation.
This commit is contained in:
@@ -215,7 +215,7 @@ class TransactionController extends Controller
|
||||
}
|
||||
|
||||
$transactions = $collector->getJournals();
|
||||
$resource = new Item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions');
|
||||
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
@@ -165,6 +165,7 @@ class TransactionRequest extends Request
|
||||
$this->emptySplitDescriptions($validator);
|
||||
$this->foreignCurrencyInformation($validator);
|
||||
$this->validateAccountInformation($validator);
|
||||
$this->validateSplitAccounts($validator);
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -368,6 +369,9 @@ class TransactionRequest extends Request
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
if(!isset($data['type'])) {
|
||||
return;
|
||||
}
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
|
||||
$sourceId = isset($transaction['source_id']) ? intval($transaction['source_id']) : null;
|
||||
@@ -404,10 +408,57 @@ class TransactionRequest extends Request
|
||||
$this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException(sprintf('The validator cannot handle transaction type "%s".', $data['type']));
|
||||
throw new FireflyException(sprintf('The validator cannot handle transaction type "%s" in validateAccountInformation().', $data['type']));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Validator $validator
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function validateSplitAccounts(Validator $validator)
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$count = isset($data['transactions']) ? count($data['transactions']) : 0;
|
||||
if ($count < 2) {
|
||||
return;
|
||||
}
|
||||
// collect all source ID's and destination ID's, if present:
|
||||
$sources = [];
|
||||
$destinations = [];
|
||||
|
||||
foreach ($data['transactions'] as $transaction) {
|
||||
$sources[] = isset($transaction['source_id']) ? intval($transaction['source_id']) : 0;
|
||||
$destinations[] = isset($transaction['destination_id']) ? intval($transaction['destination_id']) : 0;
|
||||
}
|
||||
$destinations = array_unique($destinations);
|
||||
$sources = array_unique($sources);
|
||||
// switch on type:
|
||||
switch ($data['type']) {
|
||||
case 'withdrawal':
|
||||
if (count($sources) > 1) {
|
||||
$validator->errors()->add('transactions.0.source_id', trans('validation.all_accounts_equal'));
|
||||
}
|
||||
break;
|
||||
case 'deposit':
|
||||
if (count($destinations) > 1) {
|
||||
$validator->errors()->add('transactions.0.destination_id', trans('validation.all_accounts_equal'));
|
||||
}
|
||||
break;
|
||||
case 'transfer':
|
||||
if (count($sources) > 1 || count($destinations) > 1) {
|
||||
$validator->errors()->add('transactions.0.source_id', trans('validation.all_accounts_equal'));
|
||||
$validator->errors()->add('transactions.0.destination_id', trans('validation.all_accounts_equal'));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException(sprintf('The validator cannot handle transaction type "%s" in validateSplitAccounts().', $data['type']));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
@@ -29,6 +29,7 @@ return [
|
||||
'file_already_attached' => 'Uploaded file ":name" is already attached to this object.',
|
||||
'file_attached' => 'Succesfully uploaded file ":name".',
|
||||
'must_exist' => 'The ID in field :attribute does not exist in the database.',
|
||||
'all_accounts_equal' => 'All accounts in this field must be equal.',
|
||||
'belongs_user' => 'This value is invalid for this field.',
|
||||
'at_least_one_transaction' => 'Need at least one transaction.',
|
||||
'require_currency_info' => 'The content of this field is invalid without currency information.',
|
||||
|
@@ -874,6 +874,155 @@ class TransactionControllerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to store a withdrawal with different source accounts.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\TransactionController::store
|
||||
* @covers \FireflyIII\Api\V1\Requests\TransactionRequest
|
||||
*/
|
||||
public function testFailSplitDeposit()
|
||||
{
|
||||
$account = auth()->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$second = auth()->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $account->id)->first();
|
||||
$data = [
|
||||
'description' => 'Some deposit #' . rand(1, 1000),
|
||||
'date' => '2018-01-01',
|
||||
'type' => 'deposit',
|
||||
'transactions' => [
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'destination_id' => $account->id,
|
||||
'description' => 'Part 1',
|
||||
],
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'destination_id' => $second->id,
|
||||
'description' => 'Part 2',
|
||||
],
|
||||
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/transactions', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(422);
|
||||
$response->assertExactJson(
|
||||
[
|
||||
'message' => 'The given data was invalid.',
|
||||
'errors' => [
|
||||
'transactions.0.destination_id' => [
|
||||
'All accounts in this field must be equal.',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to store a withdrawal with different source accounts.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\TransactionController::store
|
||||
* @covers \FireflyIII\Api\V1\Requests\TransactionRequest
|
||||
*/
|
||||
public function testFailSplitTransfer()
|
||||
{
|
||||
$account = auth()->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$second = auth()->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $account->id)->first();
|
||||
$data = [
|
||||
'description' => 'Some transfer #' . rand(1, 1000),
|
||||
'date' => '2018-01-01',
|
||||
'type' => 'transfer',
|
||||
'transactions' => [
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $account->id,
|
||||
'destination_id' => $second->id,
|
||||
'description' => 'Part 1',
|
||||
],
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $second->id,
|
||||
'destination_id' => $account->id,
|
||||
'description' => 'Part 2',
|
||||
],
|
||||
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/transactions', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(422);
|
||||
$response->assertExactJson(
|
||||
[
|
||||
'message' => 'The given data was invalid.',
|
||||
'errors' => [
|
||||
'transactions.0.source_id' => [
|
||||
'All accounts in this field must be equal.',
|
||||
],
|
||||
'transactions.0.destination_id' => [
|
||||
'All accounts in this field must be equal.',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to store a withdrawal with different source accounts.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\TransactionController::store
|
||||
* @covers \FireflyIII\Api\V1\Requests\TransactionRequest
|
||||
*/
|
||||
public function testFailSplitWithdrawal()
|
||||
{
|
||||
$account = auth()->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$second = auth()->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $account->id)->first();
|
||||
$data = [
|
||||
'description' => 'Some transaction #' . rand(1, 1000),
|
||||
'date' => '2018-01-01',
|
||||
'type' => 'withdrawal',
|
||||
'transactions' => [
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $account->id,
|
||||
'description' => 'Part 1',
|
||||
],
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $second->id,
|
||||
'description' => 'Part 2',
|
||||
],
|
||||
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/transactions', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(422);
|
||||
$response->assertExactJson(
|
||||
[
|
||||
'message' => 'The given data was invalid.',
|
||||
'errors' => [
|
||||
'transactions.0.source_id' => [
|
||||
'All accounts in this field must be equal.',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Show index.
|
||||
*
|
||||
@@ -1117,7 +1266,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1133,7 +1282,7 @@ class TransactionControllerTest extends TestCase
|
||||
'amount' => -10,
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]],
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -1169,7 +1318,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1185,7 +1334,7 @@ class TransactionControllerTest extends TestCase
|
||||
'amount' => -10,
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]],
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -1219,7 +1368,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1234,7 +1383,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1267,7 +1416,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1282,7 +1431,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1315,7 +1464,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1330,7 +1479,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1365,7 +1514,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1382,7 +1531,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1417,7 +1566,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1434,7 +1583,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1469,7 +1618,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1486,7 +1635,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1521,7 +1670,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1538,7 +1687,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1575,7 +1724,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1593,7 +1742,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1648,7 +1797,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertSee('I are internal ref!');
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1663,7 +1812,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1698,7 +1847,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1714,7 +1863,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1749,7 +1898,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1764,7 +1913,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1799,7 +1948,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertSee('I am a note');
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1815,7 +1964,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1850,7 +1999,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1866,7 +2015,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1901,7 +2050,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1917,7 +2066,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1951,7 +2100,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -1964,7 +2113,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => [],
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
|
||||
}
|
||||
@@ -2000,7 +2149,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -2015,7 +2164,7 @@ class TransactionControllerTest extends TestCase
|
||||
'amount' => 10,
|
||||
],
|
||||
'links' => [],
|
||||
],
|
||||
]],
|
||||
'included' => [
|
||||
0 => [
|
||||
'type' => 'piggy_bank_events',
|
||||
@@ -2059,7 +2208,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -2074,7 +2223,7 @@ class TransactionControllerTest extends TestCase
|
||||
'amount' => 10,
|
||||
],
|
||||
'links' => [],
|
||||
],
|
||||
]],
|
||||
'included' => [
|
||||
0 => [
|
||||
'type' => 'piggy_bank_events',
|
||||
@@ -2117,7 +2266,7 @@ class TransactionControllerTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -2133,10 +2282,49 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => true,
|
||||
],
|
||||
]
|
||||
]]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the data required for a split withdrawal.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\TransactionController::store
|
||||
* @covers \FireflyIII\Api\V1\Requests\TransactionRequest
|
||||
*/
|
||||
public function testSuccessStoreSplit()
|
||||
{
|
||||
$account = auth()->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$data = [
|
||||
'description' => 'Some transaction #' . rand(1, 1000),
|
||||
'date' => '2018-01-01',
|
||||
'type' => 'withdrawal',
|
||||
'transactions' => [
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $account->id,
|
||||
'description' => 'Part 1',
|
||||
],
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $account->id,
|
||||
'description' => 'Part 2',
|
||||
],
|
||||
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/transactions', $data, ['Accept' => 'application/json']);
|
||||
$json = $response->json();
|
||||
$response->assertStatus(200);
|
||||
$this->assertCount(2, $json['data']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the minimum amount of data required to create a withdrawal.
|
||||
* Add some tags as well. Expect to see them in the result.
|
||||
@@ -2176,7 +2364,7 @@ class TransactionControllerTest extends TestCase
|
||||
}
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'data' => [[
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
@@ -2191,7 +2379,7 @@ class TransactionControllerTest extends TestCase
|
||||
],
|
||||
'links' => [],
|
||||
'relationships' => [],
|
||||
],
|
||||
]],
|
||||
'included' => [],
|
||||
]
|
||||
);
|
||||
|
Reference in New Issue
Block a user