diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 5db813a..abcd991 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -8,48 +8,37 @@ * body LicensePlate License plate object to update * no response value expected for this operation **/ - - exports.modifyPlate = function(body) { return new Promise(function(resolve, reject) { - - // Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα var existingPlates = { "licensePlate": "AKH1314", // όνομα πινακίδας "id": 15, // το id της πινακίδας - }; - - if (!body.licensePlate || body.licensePlate === "" ) { - //αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("license Plate does not exist"); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // Ο έλεγχος αυτός έγινε comment διότι εκτελείται από το swagger. Συνεπώς, τον κάνω comment ώστε να μην μου χαλάει το - //coverage του modifyPlate - // if (typeof body.licensePlate !== "string"){ - // //αν το licensePlate έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - // const error = new Error("Invalid licenseplate"); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } + //Ορίζω κανόνες εγκυρότητας για τα fields της πινακίδας. + //Αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400. + //Επίσης, αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400. + // Έτσι, μειώνω το πλήθος των reject(error). + const validations = [ + { key: 'licensePlate', rule: (value) => value && value !== "", errorMessage: "license Plate does not exist" }, + { key: 'id', rule: (value) => Number.isInteger(value) && value > 0, + errorMessage: "Invalid id: must be a positive integer." } + ]; - if (!Number.isInteger(body.id) || body.id < 0) { - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; + // Έλεγχος για σφάλματα στα fields της πινακίδας + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); + return; + } } - //Αν η dummy πινακίδα που είναι ήδη καταχωρημένη μέσα στο σύστημα έχει ίδιο id με εκείνο του request body και διαφορετικό + //Αν η dummy πινακίδα που είναι ήδη καταχωρημένη μέσα στο σύστημα έχει ίδιο id με εκείνο του request body και διαφορετικό // όνομα πινακίδας με εκείνο του request body, τότε συμβαίνει με επιτυχία η τροποποίηση της πινακίδας. Αν τα γνωρίσματα της // dummy καταχωρημένης πινακίδας και του request body ταυτίζονται , τότε δεν τροποποιώ την πινακίδα αλλά αντίθετα διατηρώ // τα "παλιά" της στοιχεία.Τέλος, αν όλα τα γνωρίσματα της dummy πινακίδας διαφέρουν με τα αντίστοιχα γνωρίσματα του request @@ -72,14 +61,13 @@ exports.modifyPlate = function(body) { // Handle non-existent ID (η πινακίδα δεν υπάρχει μέσα στο σύστημα) const isNonExistent = existingPlates.id !== body.id; if (isNonExistent) { - const error = new Error("License plate doesn't exist."); + const error = new Error("License plate doesn't exist."); // Η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρέθηκε. error.response = { statusCode: 404 }; // Επιστρέφει κωδικό σφάλματος 404. - //Δηλαδή, η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρέθηκε. reject(error); return; } - resolve(); // Return the updated plate object + resolve(); }); }; @@ -92,42 +80,28 @@ exports.modifyPlate = function(body) { * no response value expected for this operation **/ exports.registerPlate = function(body) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { + //Ορίζω κανόνες εγκυρότητας για τα fields της πινακίδας. + //Αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400. + //Αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400. + // Έτσι, μειώνω το πλήθος των reject(error). + const validations = [ + { key: 'id', rule: (value) => Number.isInteger(value) && value > 0, errorMessage: 'Invalid id: must be a positive integer.' }, + { key: 'licensePlate', rule: (value) => value && value !== "", errorMessage: 'licenseplate does not exist' } + ]; - // Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα - var existingPlates = { - "licensePlate": "AKH1314", // το όνομα της πινακίδας - "id": 15, // το id της πινακίδας - - }; - - - if (!Number.isInteger(body.id) || body.id < 0) { - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - - if (!body.licensePlate || body.licensePlate === "") { - //αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("licenseplate does not exist"); - error.response = { statusCode: 400 }; - reject(error); - return; + // Έλεγχος για σφάλματα στα fields της πινακίδας + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); + return; + } } - - // Ο έλεγχος αυτός έγινε comment διότι εκτελείται από το swagger. Συνεπώς, τον κάνω comment ώστε να μην μου χαλάει το - //coverage του registerPlate - // if (typeof body.licensePlate !== "string"){ - // //αν το licensePlate έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - // const error = new Error("Invalid licenseplate"); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } + resolve(); - }); + }); }; + diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index fce6d6f..a0ab805 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -1,6 +1,3 @@ -'use strict'; - - /** * Add a new spot owner * FR15: The system administrator must be able to add a spot owner to the system @@ -8,73 +5,39 @@ * body SpotOwner Spot owner object to add * no response value expected for this operation **/ - - exports.addSpotOwner = function(body) { - return new Promise((resolve, reject) =>{ - //SpotOwner = Ιδιοκτήτης θέσεων πάρκινγκ - var existingSpotOwners = { - "id":1, // το id του ιδιοκτήτη θέσεων - "idNumber": "AK1234", // ο αριθμός ταυτότητας του ιδιοκτήτη θέσεων - "name":"John Doe", // ο ονοματεπώνυμο του ιδιοκτήτη - "email":"johhdoe@gmail.com", // το email του ιδιοκτήτη - "phone":"1234567890",// το τηλέφωνο του ιδιοκτήτη - "spots":[] // οι θέσεις του ιδιοκτήτη - }; - + return new Promise((resolve, reject) => { - if (!body.id || body.id < 0) { - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (!body.idNumber || typeof body.idNumber !== "string") { - //αν το idNumber έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid idNumber: must be a string."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (!body.name || typeof body.name !== "string") { - //αν το name έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid name: must be a string."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (!body.email || typeof body.email !== "string") { - - //αν το email έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid email: must be a string."); - error.response = { statusCode: 400 }; - reject(error); - return; - } + //idNumber = αριθμός ταυτότητας + //spots = μια λίστα που περιέχει τις θέσεις που ανήκουν στον ιδιοκτήτη θέσεων. - if (!body.phone) { - - //αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("No phone."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (!body.spots) { - - //αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("No spots."); - error.response = { statusCode: 400 }; - reject(error); - return; + // Ορίζω κανόνες εγκυρότητας για κάθε field του Ιδιοκτήτη θέσεων. + //Αν το id δεν είναι θετικός ακέραιος τότε έχω σφάλμα με κωδικό 400. + //Αν το idNumber δεν είναι string τότε έχω σφάλμα με κωδικό 400. Το ίδιο ισχύει και για τα name και email. + //Αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400. Το ίδιο ισχύει και για το spots. + const validations = [ + { key: 'id', rule: (value) => value > 0 && Number.isInteger(value), errorMessage: 'Invalid id: must be a positive integer.' }, + { key: 'idNumber', rule: (value) => typeof value === 'string' && value !== undefined + && value !== null && value.trim() !== '', errorMessage: 'Invalid idNumber: must be a string.' }, + { key: 'name', rule: (value) => typeof value === 'string' && value !== undefined + && value !== null && value.trim() !== '', errorMessage: 'Invalid name: must be a string.' }, + { key: 'email', rule: (value) => typeof value === 'string' && value !== undefined + && value !== null && value.trim() !== '', errorMessage: 'Invalid email: must be a string.' }, + { key: 'phone', rule: (value) => value !== undefined, errorMessage: 'No phone.' }, + { key: 'spots', rule: (value) => value !== undefined, errorMessage: 'No spots.' }, + ]; + + // Έλεγχος για σφάλματα στα fields του Ιδιοκτήτη θέσεων + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); + return; + } } resolve(); - }); - -} \ No newline at end of file + }); +}; \ No newline at end of file diff --git a/service/SpotService.js b/service/SpotService.js index e7f24ac..70446f3 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -11,70 +11,52 @@ exports.createSpot = function (body) { return new Promise((resolve, reject) => { - // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα - var existingSpots = { - "address": "Navarinou 18", - "id": 15, - "type": "Garage", - "chargerAvailability": false + var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα + "address": "Navarinou 18", // η διεύθυνση της θέσης + "id": 15, // το id της θέσης + "type": "Garage", // ο τύπος της θέσης + "chargerAvailability": false // η διαθεσιμότητα φορτιστή της θέσης }; - // Το address πρέπει να είναι string - if (!body.address) { - //αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid address: must be a string."); - error.response = { statusCode: 400 }; - reject(error); - return; + // Ορίζω τους κανόνες εγκυρότητας για κάθε field της θέσης πάρκινγκ. Το κάνω αυτό για να μειώσω το πλήθος των reject(error) + const validations = [ + { key: 'address', rule: (value) => value !== undefined && value !== null && typeof value === 'string' + && value.trim() !== '', errorMessage: "Invalid address: must be a string." }, // Η διεύθυνση είναι string + { key: 'id', rule: (value) => value >= 0 && Number.isInteger(value), // το id είναι μη αρνητικός ακέραιος αριθμός + errorMessage: "Invalid id: must be a non-negative integer." }, + { key: 'type', rule: (value) => ["Garage", "Open", "Underground"].includes(value), // ο τύπος μπορεί να πάρει 3 διαφορετικές τιμές + errorMessage: "Invalid type: must be one of 'Garage', 'Open', or 'Underground'." }, + { key: 'chargerAvailability', rule: (value) => typeof value === 'boolean', // Η διαθεσιμότητα φορτιστή είναι boolean + errorMessage: "Invalid chargerAvailability: must be a boolean." } + ]; + + // Έλεγχος για σφάλματα στα fields της θέσης πάρκινγκ + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); + return; + } } - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός - if (!body.id || body.id < 0) { - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" - if (!["Garage", "Open", "Underground"].includes(body.type)) { - //αν το type έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - //Το chargerAvailability πρέπει να είναι boolean - if (typeof body.chargerAvailability !== 'boolean') { - //αν το chargerAvailability έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid chargerAvailability: must be a boolean."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // Έλεγχος αν υπάρχει ήδη το spot , δηλαδή γίνεται έλεγχος για διπλότυπη θέση + // Έλεγχος αν υπάρχει ήδη το spot , δηλαδή γίνεται έλεγχος για διπλότυπη θέση. // Αν όλα τα attributes του existingSpots ισούται ένα προς ένα με όλα τα attributes του spot που θέλω να φτιάξω, // τότε θέλω να δημιουργήσω μια διπλότυπη θέση. Άρα, έχω σφάλμα - const spotExists = existingSpots.id === body.id && - existingSpots.address === body.address && - existingSpots.type === body.type && + const spotExists = existingSpots.id === body.id && + existingSpots.address === body.address && + existingSpots.type === body.type && existingSpots.chargerAvailability === body.chargerAvailability; - - if (spotExists) { - //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Spot already exists: a spot with the same attributes already exists."); - error.response = { statusCode: 400 }; + + if (spotExists) { //Αν βρεθεί διπλότυπη θέση + const error = new Error("Spot already exists: a spot with the same attributes already exists."); // έχω σφάλμα + error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); return; } - // Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την προσθέσω - // Δεν το κάνω, καθώς η συνάρτηση είναι dummy - - resolve(); + resolve();// Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την προσθέσω. Δεν το κάνω, καθώς η συνάρτηση είναι dummy. }); }; @@ -86,7 +68,7 @@ exports.createSpot = function (body) { * returns List **/ exports.getSpots = function () { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve, /**reject*/) { // reject is unused var examples = {}; examples['application/json'] = [{ "address": "address", @@ -119,122 +101,62 @@ exports.getSpots = function () { * id Integer * no response value expected for this operation **/ -// exports.modifySpot = function (body, address, type, charger, id) { -// return new Promise(function (resolve, reject) { -// resolve(); -// }); -// } exports.modifySpot = function (body, address, type, charger, id) { - return new Promise((resolve, reject) => { // Οι έλεγχοι που έγιναν comment εκτελούνται από το swagger, - // επομένως είναι περιττό να τους γράψω μέσα στο modifySpot διότι μου χαλάνε το coverage του SpotService.js + return new Promise((resolve, reject) => { - // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα - var existingSpots = { - address: "Navarinou 18", - id: 15, - type: "Garage", - charger: false, + var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα + address: "Navarinou 18", // η διεύθυνση της θέσης + id: 15, // το id της θέσης + type: "Garage", // ο τύπος της θέσης + charger: false, // η διαθεσιμότητα φορτιστή της θέσης }; - // Έλεγχος query παραμέτρων - - // // Το address πρέπει να είναι string - // if (!address) { - // //αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - // const error = new Error("Invalid address in query: must be a string."); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } - - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" - if (!["Garage", "Open", "Underground"].includes(type)) { - //αν το type έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // //Το charger πρέπει να είναι boolean - // if (typeof charger !== 'boolean') { - // //αν το charger έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - // const error = new Error("Invalid charger in query: must be a boolean."); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } - - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός - if (!id || id < 0) { - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid id in query: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // // Έλεγχος request body - // if (!body || typeof body !== 'object') { - // const error = new Error("Invalid request body."); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } - - // Σύγκριση query παραμέτρων με request body attributes. - // Τα αντίστοιχα query attributes με τα αντίστοιχα attributes του request body πρέπει να ταυτίζονται. - if (body.address !== address) { - const error = new Error("Address mismatch between query and body."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (body.type !== type) { - const error = new Error("Type mismatch between query and body."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (body.charger !== charger) { - const error = new Error("Charger mismatch between query and body."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - if (body.id !== id) { - const error = new Error("ID mismatch between query and body."); - error.response = { statusCode: 400 }; - reject(error); - return; + //Ορίζω κανόνες εγκυρότητας για το type και το id της θέσης πάρκινγκ. Τα address και charger ελέγχονται από το swagger. + //Επίσης, ορίζω κανόνες εγκυρότητας για τη σύγκριση της ισότητας των query και body παραμέτρων. + //Το κάνω αυτό για να μειώσω το πλήθος των reject(error). + const validations = [ + { condition: () => ["Garage", "Open", "Underground"].includes(type), // ο τύπος μπορεί να πάρει 3 διαφορετικές τιμές + errorMessage: "Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'." }, + { condition: () => id && id >= 0, // το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός + errorMessage: "Invalid id in query: must be a positive integer." }, + { condition: () => body.address === address, // Τα address των query και body πρέπει να ταυτίζονται + errorMessage: "Address mismatch between query and body." }, + { condition: () => body.type === type, // Τα type των query και body πρέπει να ταυτίζονται + errorMessage: "Type mismatch between query and body." }, + { condition: () => body.charger === charger, // Τα charger των query και body πρέπει να ταυτίζονται + errorMessage: "Charger mismatch between query and body." }, + { condition: () => body.id === id, // Τα id των query και body πρέπει να ταυτίζονται + errorMessage: "ID mismatch between query and body." }, + ]; + + // Έλεγχος για σφάλματα με βάση τους παραπάνω κανόνες εγκυρότητας + for (const { condition, errorMessage } of validations) { + if (!condition()) { // Αν δεν ικανοποιείται το condition, τότε υπάρχει σφάλμα + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; // Επιστρέφει 400 + reject(error); + return; + } } // Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" στοιχεία της θέσης. //Ουσιαστικά, δεν γίνεται τροποποίηση. - const spotExists = //Δεν ελέγχω τα id του existingSpots και του body διότι θεωρώ εκ των πραγμάτων ότι ισούται μεταξύ τους. // Αυτό συμβαίνει διότι κάνω modify μια ήδη υπάρχουσα θέση , άρα το id δεν αλλάζει κατά το modification. // Αν άλλαζε το id τότε θα έκανα create μια νέα θέση. // Αντίστοιχος έλεγχος έγινε και στο PUT /licensePlate. - //Επίσης αυτός ο έλεγχος αφορά και το PUT /reservation , αλλά δεν γράφτηκε για λόγους συντομίας. - existingSpots.address === body.address && - existingSpots.type === body.type && - existingSpots.charger === body.charger; + const spotExists = existingSpots.address === body.address && + existingSpots.type === body.type && + existingSpots.charger === body.charger; - if (spotExists) { - const error = new Error("Spot already exists: a spot with the same attributes already exists."); - error.response = { statusCode: 400 }; + if (spotExists) { //Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" της στοιχεία + const error = new Error("Spot already exists: a spot with the same attributes already exists."); // Αν ναι , τότε σφάλμα + error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); return; } - // Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την τροποποιήσω - // Δεν το κάνω, καθώς η συνάρτηση είναι dummy - - resolve(); + resolve();// Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την τροποποιήσω. Δεν το κάνω, καθώς η συνάρτηση είναι dummy. }); }; @@ -248,7 +170,7 @@ exports.modifySpot = function (body, address, type, charger, id) { **/ exports.removeSpot = function (id) { return new Promise(function (resolve, reject) { - resolve(); + resolve(); // Διαγράφει τη θέση από το σύστημα }); } @@ -284,6 +206,7 @@ exports.searchSpot = function(address,type,charger) { status: 404, message: "No matching parking spots found.", }) + } }); } \ No newline at end of file diff --git a/test/POST_payment.test.js b/test/POST_payment.test.js index 3f4f8a5..51bd0df 100644 --- a/test/POST_payment.test.js +++ b/test/POST_payment.test.js @@ -20,35 +20,36 @@ test.after.always((t) => { //amount = το χρηματικό ποσό της πληρωμής //Από το yaml αρχείο παρατηρώ ότι το amount μπορεί να είναι μόνο ακέραιος , αλλιώς έχω σφάλμα. - -//Τεστάρω την συνάρτηση makeReservation για την επιτυχή δημιουργία μιας καινούριας πληρωμής -test("makePayment function succeeds with valid data", async (t) => { - const body = await makePayment({ - id: 123, - user: { - id: 456, - name: "Jane Doe", - licensePlate: { +//Happy path για το payment +test("POST /payment succesfull", async (t) => { + const {body, statusCode} = await t.context.got.post('payment', { + json : { + id: 123, + user: { + id: 456, + name: "Jane Doe", + licensePlate: { + userId: 456, + licensePlate: "XYZ-7890", + }, + reservation: [ + { + id: 789, + spotId: 321, userId: 456, - licensePlate: "XYZ-7890", + startTime: "2024-11-23T10:00:00Z", + duration: "2024-11-23T12:00:00Z", + date: "2024-11-23", }, - reservation: [ - { - id: 789, - spotId: 321, - userId: 456, - startTime: "2024-11-23T10:00:00Z", - duration: "2024-11-23T12:00:00Z", - date: "2024-11-23", - }, - ], - }, - amount: 150, - }); - - t.falsy(body); // το body είναι κενό. - }); - + ], + }, + amount: 150, + } +}); + + t.is(statusCode, 200); //checking that the status code is 200 + t.falsy(body); //confirming that the response body is empty +}); //Τεστ για μη έγκυρα δεδομένα στο payment test("POST /payment with invalid data returns 400", async (t) => { @@ -129,4 +130,4 @@ test("POST /payment with missing id returns 400", async (t) => { }, }); t.is(body.statusCode, 400); // To τεστ αποτυχαίνει και επιστρέφει κωδικό αποτυχίας 400. -}); +}); \ No newline at end of file diff --git a/test/POST_reservation.test.js b/test/POST_reservation.test.js index dc6369a..68d25d3 100644 --- a/test/POST_reservation.test.js +++ b/test/POST_reservation.test.js @@ -30,6 +30,20 @@ test("makeReservation function checks for a duplicate reservation", async (t) => t.is(body.response.statusCode, 400); }); +test("POST /reservation with correct data returns code 200", async (t) => { + const { body, statusCode } = await t.context.got.post('reservation', { + json: { + date: "2024-11-19", + startTime: "2024-11-19T08:00:00Z", + duration: "2024-11-19T11:00:00Z", + spotId: 1, + id: 1, + userId: 1 + } + }); + t.is(statusCode, 200); +}); + //Τεστάρω αν δεδομένα της κράτησης είναι έγκυρα test("POST /reservation with invalid data returns 400", async (t) => { // Σε αυτό το τεστ , τα δεδομένα της κράτησης είναι μη έγκυρα. @@ -88,5 +102,4 @@ test("POST /reservation with missing duration returns 400", async (t) => { json: { date:"2024-11-19", startTime: "2024-11-19T08:00:00Z", userId:1, spotId: 1, id:1 } }); t.is(body.response.statusCode, 400); -}); - +}); \ No newline at end of file