From 8833fca7348882bc17c86eb74b538e3f55b72d89 Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Thu, 21 Sep 2023 17:52:13 -0500 Subject: [PATCH] Adding in tests for the endpoints after creation Having tests on our code is essential, but it's equally crucial to ensure that all endpoints function correctly with each deployment. Once these validations are in place, we can integrate them into Jenkins during the build process to ensure that our releases are free of bugs. --- ...sessment API Tests.postman_collection.json | 739 ++++++++++++++++++ 1 file changed, 739 insertions(+) create mode 100644 Postman-backend-dev-tests/Damage Assessment API Tests.postman_collection.json diff --git a/Postman-backend-dev-tests/Damage Assessment API Tests.postman_collection.json b/Postman-backend-dev-tests/Damage Assessment API Tests.postman_collection.json new file mode 100644 index 0000000..b7dad9c --- /dev/null +++ b/Postman-backend-dev-tests/Damage Assessment API Tests.postman_collection.json @@ -0,0 +1,739 @@ +{ + "info": { + "_postman_id": "1facadda-be71-4b53-b028-3100e261e0d3", + "name": "Damage Assessment API Tests", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "2845111" + }, + "item": [ + { + "name": "Setup", + "item": [ + { + "name": "New Request", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Domain variable is set\", function() {", + " pm.expect(pm.environment.has(\"domain\")).to.be.true;", + " pm.expect(pm.environment.get(\"domain\")).to.be.not.empty;", + "});" + ], + "type": "text/javascript" + } + }, + { + "listen": "prerequest", + "script": { + "exec": [ + "if (!pm.environment.has(\"domain\") || !pm.environment.get(\"domain\")) {", + " pm.environment.set(\"domain\", \"https://dev-services.damageassessment.net/\");", + "}" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "https://dev-services.damageassessment.net/answers", + "protocol": "https", + "host": [ + "dev-services", + "damageassessment", + "net" + ], + "path": [ + "answers" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Answers", + "item": [ + { + "name": "Get Answers", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "pm.test(\"No errors in Pre-request Script\", function() {", + " pm.expect(pm.environment.get(\"preRequestError\")).to.be.undefined;", + "});", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;", + "pm.test(\"Response status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});", + "", + "", + "pm.test(\"Response is an array\", function () {", + " pm.expect(pm.response.json()).to.be.an('array');", + "});", + "", + "", + "pm.test(\"Response array is empty\", function () {", + " const responseData = pm.response.json();", + " ", + " pm.expect(responseData).to.be.an('array');", + " pm.expect(responseData).to.have.lengthOf(0);", + "});", + "", + "", + "pm.test(\"Response time is less than 200ms\", function () {", + " pm.expect(pm.response.responseTime).to.be.below(200);", + "});", + "", + "", + "pm.test(\"Response does not contain any errors or error messages\", function () {", + " const responseData = pm.response.json();", + "", + " pm.expect(responseData).to.be.an('array');", + " pm.expect(responseData).to.have.lengthOf(0, \"Response should not contain any errors or error messages\");", + "});" + ], + "type": "text/javascript" + } + }, + { + "listen": "prerequest", + "script": { + "exec": [ + "if (!pm.environment.get(\"domain\")) {", + " console.error(\"Error: Domain is missing!\");", + "", + " // Set an error flag", + " pm.environment.set(\"preRequestError\", \"Domain is missing!\");", + "", + " // Change request URL to prevent it from sending", + " pm.request.url = \"http://invalid-url-to-prevent-sending.com\";", + "}" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}answers", + "host": [ + "{{domain}}answers" + ] + } + }, + "response": [] + }, + { + "name": "Put Answers", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": 1,\n \"questionId\": 1,\n \"answerText\": \"string\",\n \"comment\": \"string\",\n \"surveyResponseId\": 1\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{domain}}answers", + "host": [ + "{{domain}}answers" + ] + } + }, + "response": [] + }, + { + "name": "Post Answers", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": 1,\n \"questionId\": 1,\n \"answerText\": \"string\",\n \"comment\": \"string\",\n \"surveyResponseId\": 1\n}" + }, + "url": { + "raw": "{{domain}}answers", + "host": [ + "{{domain}}answers" + ] + } + }, + "response": [] + }, + { + "name": "Get Answers By Id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}answers/1", + "host": [ + "{{domain}}answers" + ], + "path": [ + "1" + ] + } + }, + "response": [] + }, + { + "name": "Get Answers By Reponse Id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}answers/byresponse/1", + "host": [ + "{{domain}}answers" + ], + "path": [ + "byresponse", + "1" + ] + } + }, + "response": [] + }, + { + "name": "Get Answers By Question Id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}answers/byquestion/1", + "host": [ + "{{domain}}answers" + ], + "path": [ + "byquestion", + "1" + ] + } + }, + "response": [] + }, + { + "name": "Delete Answers by Id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{domain}}answers/1", + "host": [ + "{{domain}}answers" + ], + "path": [ + "1" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Employees", + "item": [ + { + "name": "Get Employees", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "", + "pm.test(\"No errors in Pre-request Script\", function() {", + " pm.expect(pm.environment.get(\"preRequestError\")).to.be.undefined;", + "});", + "", + "", + "tests[\"Response code is 200 OK\"] = responseCode.code === 200;", + "", + "// This assumes jsonData.items is sorted and you want the next ID.", + "var nextId;", + "if (jsonData && jsonData.length > 0) {", + " nextId = _.last(jsonData).id + 1;", + " pm.environment.set(\"nextEmployeeId\", nextId);", + "} else {", + " console.log(\"No items found in the response or invalid response format\");", + "}", + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "prerequest", + "script": { + "exec": [ + "if (!pm.environment.get(\"domain\")) {", + " console.error(\"Error: Domain is missing!\");", + "", + " // Set an error flag", + " pm.environment.set(\"preRequestError\", \"Domain is missing!\");", + "", + " // Change request URL to prevent it from sending", + " pm.request.url = \"http://invalid-url-to-prevent-sending.com\";", + "}" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}employees", + "host": [ + "{{domain}}employees" + ] + } + }, + "response": [] + }, + { + "name": "Post Employees", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;", + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "prerequest", + "script": { + "exec": [ + "function pad(number, digits) {", + " return String(number).padStart(digits, '0');", + "}", + "", + "// Random year between 1980 and 2000", + "var randomYear = Math.floor(Math.random() * (2000 - 1980 + 1) + 1980);", + "", + "// Random month between 1 and 12", + "var randomMonth = Math.floor(Math.random() * 12) + 1;", + "", + "// Days per month (considering leap year for February)", + "var daysInMonth = [31, (randomYear % 4 === 0 && (randomYear % 100 !== 0 || randomYear % 400 === 0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];", + "", + "// Random day based on month", + "var randomDay = Math.floor(Math.random() * daysInMonth[randomMonth - 1]) + 1;", + "", + "// Random time components", + "var randomHour = Math.floor(Math.random() * 24);", + "var randomMinute = Math.floor(Math.random() * 60);", + "var randomSecond = Math.floor(Math.random() * 60);", + "var randomMillisecond = Math.floor(Math.random() * 10000000); // 7 digits", + "", + "// Construct the date string", + "var randomBirthDate = randomYear + '-' +", + " pad(randomMonth, 2) + '-' +", + " pad(randomDay, 2) + 'T' +", + " pad(randomHour, 2) + ':' +", + " pad(randomMinute, 2) + ':' +", + " pad(randomSecond, 2) + '.' +", + " pad(randomMillisecond, 7) + '+00:00';", + "", + "// Array of possible languages", + "var languages = [\"en\", \"fr\", \"de\", \"ht\", \"es\"];", + "var randomLanguage = languages[Math.floor(Math.random() * languages.length)];", + "", + "// Use Postman's built-in dynamic variables", + "var firstName = pm.variables.replaceIn(\"{{$randomFirstName}}\");", + "var lastName = pm.variables.replaceIn(\"{{$randomLastName}}\");", + "var phoneNumber = pm.variables.replaceIn(\"{{$randomPhoneNumber}}\");", + "var email = pm.variables.replaceIn(\"{{$randomEmail}}\");", + "var randomBoolean = pm.variables.replaceIn(\"{{$randomBoolean}}\"); // This will return \"true\" or \"false\" as a string", + "", + "// Set the environment variables", + "pm.environment.set(\"employeeName\", firstName + \" \" + lastName);", + "pm.environment.set(\"randomPhoneNumber\", phoneNumber);", + "pm.environment.set(\"randomBirthDate\", randomBirthDate);", + "pm.environment.set(\"randomEmail\", email);", + "pm.environment.set(\"randomBoolean\", randomBoolean);", + "pm.environment.set(\"randomLanguage\", randomLanguage);", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": {{nextEmployeeId}},\n \"employeeCode\": \"{{$randomBankAccount}}\",\n \"name\": \"{{employeeName}}\",\n \"birthDate\": \"{{randomBirthDate}}\",\n \"officePhoneNumber\": \"{{randomPhoneNumber}}\",\n \"email\": \"{{randomEmail}}\",\n \"isActive\": {{$randomBoolean}},\n \"preferredLanguage\": \"{{randomLanguage}}\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{domain}}employees", + "host": [ + "{{domain}}employees" + ] + } + }, + "response": [] + }, + { + "name": "Get Employees By Id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"Response code is 200 OK\"] = responseCode.code === 200;", + "", + "pm.test(\"Employee name matches\", function() {", + " var expectedName = pm.environment.get(\"employeeName\");", + " pm.expect(jsonData.name).to.eql(expectedName);", + "});", + "", + "pm.test(\"Birth date matches\", function() {", + " var expectedBirthDate = pm.environment.get(\"randomBirthDate\");", + " pm.expect(jsonData.birthDate).to.eql(expectedBirthDate);", + "});", + "", + "pm.test(\"Phone number matches\", function() {", + " var expectedPhoneNumber = pm.environment.get(\"randomPhoneNumber\");", + " pm.expect(jsonData.officePhoneNumber).to.eql(expectedPhoneNumber);", + "});", + "", + "pm.test(\"Email matches\", function() {", + " var expectedEmail = pm.environment.get(\"randomEmail\");", + " pm.expect(jsonData.email).to.eql(expectedEmail);", + "});", + "", + "pm.test(\"Activity status matches\", function() {", + " var expectedIsActive = pm.environment.get(\"randomBoolean\");", + " pm.expect(jsonData.isActive).to.eql(expectedIsActive === \"true\");", + "});", + "", + "pm.test(\"Preferred language matches\", function() {", + " var expectedLanguage = pm.environment.get(\"randomLanguage\");", + " pm.expect(jsonData.preferredLanguage).to.eql(expectedLanguage);", + "});", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}employees/{{nextEmployeeId}}", + "host": [ + "{{domain}}employees" + ], + "path": [ + "{{nextEmployeeId}}" + ] + } + }, + "response": [] + }, + { + "name": "Put Employees", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"200 Unauthorized POST request\"] = responseCode.code === 200;", + "" + ], + "type": "text/javascript" + } + }, + { + "listen": "prerequest", + "script": { + "exec": [ + "function pad(number, digits) {", + " return String(number).padStart(digits, '0');", + "}", + "", + "// Random year between 1980 and 2000", + "var randomYear = Math.floor(Math.random() * (2000 - 1980 + 1) + 1980);", + "", + "// Random month between 1 and 12", + "var randomMonth = Math.floor(Math.random() * 12) + 1;", + "", + "// Days per month (considering leap year for February)", + "var daysInMonth = [31, (randomYear % 4 === 0 && (randomYear % 100 !== 0 || randomYear % 400 === 0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];", + "", + "// Random day based on month", + "var randomDay = Math.floor(Math.random() * daysInMonth[randomMonth - 1]) + 1;", + "", + "// Random time components", + "var randomHour = Math.floor(Math.random() * 24);", + "var randomMinute = Math.floor(Math.random() * 60);", + "var randomSecond = Math.floor(Math.random() * 60);", + "var randomMillisecond = Math.floor(Math.random() * 10000000); // 7 digits", + "", + "// Construct the date string", + "var randomBirthDate = randomYear + '-' +", + " pad(randomMonth, 2) + '-' +", + " pad(randomDay, 2) + 'T' +", + " pad(randomHour, 2) + ':' +", + " pad(randomMinute, 2) + ':' +", + " pad(randomSecond, 2) + '.' +", + " pad(randomMillisecond, 7) + '+00:00';", + "", + "// Array of possible languages", + "var languages = [\"en\", \"fr\", \"de\", \"ht\", \"es\"];", + "var randomLanguage = languages[Math.floor(Math.random() * languages.length)];", + "", + "// Use Postman's built-in dynamic variables", + "var firstName = pm.variables.replaceIn(\"{{$randomFirstName}}\");", + "var lastName = pm.variables.replaceIn(\"{{$randomLastName}}\");", + "var phoneNumber = pm.variables.replaceIn(\"{{$randomPhoneNumber}}\");", + "var email = pm.variables.replaceIn(\"{{$randomEmail}}\");", + "var randomBoolean = pm.variables.replaceIn(\"{{$randomBoolean}}\"); // This will return \"true\" or \"false\" as a string", + "", + "// Set the environment variables", + "pm.environment.set(\"employeeName\", firstName + \" \" + lastName);", + "pm.environment.set(\"randomPhoneNumber\", phoneNumber);", + "pm.environment.set(\"randomBirthDate\", randomBirthDate);", + "pm.environment.set(\"randomEmail\", email);", + "pm.environment.set(\"randomBoolean\", randomBoolean);", + "pm.environment.set(\"randomLanguage\", randomLanguage);", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": {{nextEmployeeId}},\n \"employeeCode\": \"{{$randomBankAccount}}\",\n \"name\": \"{{employeeName}}\",\n \"birthDate\": \"{{randomBirthDate}}\",\n \"officePhoneNumber\": \"{{randomPhoneNumber}}\",\n \"email\": \"{{randomEmail}}\",\n \"isActive\": {{$randomBoolean}},\n \"preferredLanguage\": \"{{randomLanguage}}\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{domain}}employees", + "host": [ + "{{domain}}employees" + ] + } + }, + "response": [] + }, + { + "name": "Get Employees By Id", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"Response code is 200 OK\"] = responseCode.code === 200;", + "", + "pm.test(\"Employee name matches\", function() {", + " var expectedName = pm.environment.get(\"employeeName\");", + " pm.expect(jsonData.name).to.eql(expectedName);", + "});", + "", + "pm.test(\"Birth date matches\", function() {", + " var expectedBirthDate = pm.environment.get(\"randomBirthDate\");", + " pm.expect(jsonData.birthDate).to.eql(expectedBirthDate);", + "});", + "", + "pm.test(\"Phone number matches\", function() {", + " var expectedPhoneNumber = pm.environment.get(\"randomPhoneNumber\");", + " pm.expect(jsonData.officePhoneNumber).to.eql(expectedPhoneNumber);", + "});", + "", + "pm.test(\"Email matches\", function() {", + " var expectedEmail = pm.environment.get(\"randomEmail\");", + " pm.expect(jsonData.email).to.eql(expectedEmail);", + "});", + "", + "pm.test(\"Activity status matches\", function() {", + " var expectedIsActive = pm.environment.get(\"randomBoolean\");", + " pm.expect(jsonData.isActive).to.eql(expectedIsActive === \"true\");", + "});", + "", + "pm.test(\"Preferred language matches\", function() {", + " var expectedLanguage = pm.environment.get(\"randomLanguage\");", + " pm.expect(jsonData.preferredLanguage).to.eql(expectedLanguage);", + "});", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{domain}}employees/{{nextEmployeeId}}", + "host": [ + "{{domain}}employees" + ], + "path": [ + "{{nextEmployeeId}}" + ] + } + }, + "response": [] + }, + { + "name": "Delete Employees", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = JSON.parse(responseBody);", + "", + "tests[\"Response code is 200 OK\"] = responseCode.code === 200;", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{domain}}employees/{{nextEmployeeId}}", + "host": [ + "{{domain}}employees" + ], + "path": [ + "{{nextEmployeeId}}" + ] + } + }, + "response": [] + } + ] + } + ] +} \ No newline at end of file