ソースを参照

Added 404 when adding credential for not existing domain

Lukas Metzger 7 年 前
コミット
15ff44a86e

+ 3 - 0
backend/src/controllers/Credentials.php

@@ -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);
         }
     }
 

+ 8 - 0
backend/src/operations/Credentials.php

@@ -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);

+ 13 - 0
backend/test/tests/credentials-crud.js

@@ -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',