Added 404 when adding credential for not existing domain

This commit is contained in:
Lukas Metzger 2018-04-02 13:35:14 +02:00
parent dcc0989d6d
commit 15ff44a86e
3 changed files with 24 additions and 0 deletions

View file

@ -79,6 +79,9 @@ class Credentials
} catch (\Exceptions\InvalidKeyException $e) {
$this->logger->debug('User tries to add invalid credential key.');
return $res->withJson(['error' => 'The provided key is invalid.'], 400);
} catch (\Exceptions\NotFoundException $e) {
$this->logger->debug('User tries to add credential for not existing record.');
return $res->withJson(['error' => 'The provided record does not exist.'], 404);
}
}

View file

@ -98,6 +98,14 @@ class Credentials
$this->db->beginTransaction();
$query = $this->db->prepare('SELECT id FROM records WHERE id=:recordId');
$query->bindValue(':recordId', $record, \PDO::PARAM_INT);
$query->execute();
if ($query->fetch() === false) {
$this->db->rollBack();
throw new \Exceptions\NotFoundException();
}
$query = $this->db->prepare('INSERT INTO remote (record, description, type, security) VALUES (:record, :description, :type, :security)');
$query->bindValue(':record', $record, \PDO::PARAM_INT);
$query->bindValue(':description', $description, \PDO::PARAM_STR);

View file

@ -62,6 +62,19 @@ test.run(async function () {
assert.equal(res.status, 400);
//Test invalid record
var res = await req({
url: '/records/100/credentials',
method: 'post',
data: {
description: 'Test',
type: 'password',
password: 'foo'
}
});
assert.equal(res.status, 404, 'Not existent record should trigger error.');
//Add key (key is intensionally very short but valid) and get it
var res = await req({
url: '/records/1/credentials',