Commit fa2ff212 authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch '10-adicionar-rota-para-inserir-formulario' into 'develop'

Resolve "Adicionar rota para inserir formulário"

Closes #10

See merge request !14
parents c451d0a8 d6d0960b
Pipeline #20136 passed with stages
in 1 minute and 15 seconds
......@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.0.13 - 26-04-2019
### Added
- A route to POST a form #10 (Horstmann)
- Tests on route POST (Horstmann)
### Changed
- dbHandler's tests to suit with new forms and inputs insertion
## 0.0.12 - 25-04-2019
### Added
- OptHandler to standardize constructors from Forms and Inputs #19 (Horstmann)
......
{
"name": "form-creator-api",
"version": "0.0.12",
"version": "0.0.13",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
......@@ -36,7 +36,6 @@
.get("/forms")
.expect(200)
.expect((res: any) => {
expect(res.body).to.be.an("array");
expect(res.body[0].id).to.be.equal(1);
expect(res.body[0].title).to.be.equal("Form Title 1");
......@@ -113,7 +112,7 @@
.end(done);
});
it("should respond 500 when getting inexistent item", (done) => {
it("should respond 500 when getting inexistent form", (done) => {
request(server)
.get("/forms/10")
.expect(500)
......@@ -127,4 +126,92 @@
.end(done);
});
it("should respond 200 when posting valid form", (done) => {
request(server)
.post("/forms")
.send({
title: 'Form Title 4'
, description: 'Form Description 4'
, inputs:[
{
placement: 0
, description: 'Description Question 1 Form 4'
, question: 'Question 1 Form 4'
, type: 1
, validation: []
}
, {
placement: 1
, description: 'Description Question 2 Form 4'
, question: 'Question 2 Form 4'
, type: 1
, validation: [
{ type: 3, arguments: [ '10' ] }
, { type: 4, arguments: [ '2' ] }
]
}
, {
placement: 2
, description: 'Description Question 3 Form 4'
, question: 'Question 3 Form 4'
, type: 1
, validation: [
{ type: 1, arguments: [ '\\d{5}-\\d{3}' ] }
, { type: 2, arguments: [] }
]
}
]
})
.expect(200)
.end(done);
});
it("should respond 500 when posting malformed form", (done) => {
request(server)
.post("/forms")
.send({
description: 'Form Description 4'
, inputs:[
{
placement: 0
, description: 'Description Question 1 Form 4'
, question: 'Question 1 Form 4'
, type: 1
, validation: []
}
, {
placement: 1
, description: 'Description Question 2 Form 4'
, question: 'Question 2 Form 4'
, type: 1
, validation: [
{ type: 3, arguments: [ '10' ] }
, { type: 4, arguments: [ '2' ] }
]
}
, {
placement: 2
, description: 'Description Question 3 Form 4'
, question: 'Question 3 Form 4'
, type: 1
, validation: [
{ type: 1, arguments: [ '\\d{5}-\\d{3}' ] }
, { type: 2, arguments: [] }
]
}
]
})
.expect(500)
.expect((res: any) => {
const message = "Could not insert form. Some error has occurred. Check error property for details.";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.message).to.be.equal(message);
})
.end(done);
});
});
......@@ -19,7 +19,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Form } from "../../core/form";
import { OptHandler } from "../../utils/optHandler";
import { Form, FormOptions} from "../../core/form";
import { Response, NextFunction } from "express";
import { Request } from "../apiTypes";
......@@ -66,4 +67,32 @@ export class FormCtrl {
}
public static write(req: Request, res: Response, next: NextFunction) {
try{
const form: Form = new Form(OptHandler.form(req.body));
req.db.writeForm(form, (err: Error, formResult: Form) => {
if (err){
throw err;
}
else{
res.json({
id: formResult.id
, message: "Form added. Id on key 'id'"
});
return;
}
});
}
catch(e){
res.status(500).json({
message: "Could not insert form. Some error has occurred. Check error property for details.",
error: e.message
});
return
}
}
}
......@@ -49,6 +49,7 @@ app.use("/", DbHandlerMw());
app.get("/forms/", FormCtrl.list);
app.get("/forms/:id", FormCtrl.read);
app.post("/forms", FormCtrl.write);
// Listening
......
......@@ -35,7 +35,7 @@ describe("Database Handler", () => {
it("should insert a form", (done) => {
dbhandler.executeQuery("INSERT INTO form(id, title, description)\
VALUES\
(4, 'Form Title 4', 'Form Description 4');", (err: Error, result?: QueryResult) => {
(5, 'Form Title 5', 'Form Description 5');", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("INSERT");
expect(result.rowCount).to.be.equal(1);
......@@ -52,7 +52,7 @@ describe("Database Handler", () => {
(callback: (err: Error, result?: QueryResult) => void) => {
dbhandler.executeQuery("INSERT INTO form(id, title, description)\
VALUES\
(5, 'Form Title 5', 'Form Description 6');", callback);
(6, 'Form Title 6', 'Form Description 6');", callback);
},
(cb: (err: Error, result?: QueryResult) => void) => {
dbhandler.rollback(cb);
......@@ -73,13 +73,13 @@ describe("Database Handler", () => {
dbhandler.executeQuery("SELECT * FROM form;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("SELECT");
expect(result.rowCount).to.be.equal(4);
expect(result.rowCount).to.be.equal(5);
done();
});
});
it("should remove non existent form", (done) => {
dbhandler.executeQuery("DELETE FROM form WHERE id=5;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM form WHERE id=6;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(0);
......@@ -89,7 +89,7 @@ describe("Database Handler", () => {
});
it("should remove existent form", (done) => {
dbhandler.executeQuery("DELETE FROM form WHERE id=4;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM form WHERE id=5;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(1);
......@@ -101,8 +101,8 @@ describe("Database Handler", () => {
it("should insert a input", (done) => {
dbhandler.executeQuery("INSERT INTO input(id, id_form, placement, input_type, question, description)\
VALUES\
(9, 2, 3,'TEXT', 'Question 3 Form 2', 'Description Question 3 Form 2'),\
(10, 2, 4,'TEXT', 'Question 4 Form 2', 'Description Question 4 Form 2');", (err: Error, result?: QueryResult) => {
(12, 2, 3,'TEXT', 'Question 3 Form 2', 'Description Question 3 Form 2'),\
(13, 2, 4,'TEXT', 'Question 4 Form 2', 'Description Question 4 Form 2');", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("INSERT");
expect(result.rowCount).to.be.equal(2);
......@@ -114,14 +114,14 @@ describe("Database Handler", () => {
dbhandler.executeQuery("SELECT * FROM input;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("SELECT");
expect(result.rowCount).to.be.equal(9);
expect(result.rowCount).to.be.equal(12);
done();
});
});
it("should remove non existent input", (done) => {
dbhandler.executeQuery("DELETE FROM input WHERE id=11;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM input WHERE id=14;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(0);
......@@ -130,7 +130,7 @@ describe("Database Handler", () => {
});
it("should remove existent input", (done) => {
dbhandler.executeQuery("DELETE FROM input WHERE id=9 OR id=10;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM input WHERE id=12 OR id=13;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(2);
......@@ -142,8 +142,8 @@ describe("Database Handler", () => {
it("should insert a input validations", (done) => {
dbhandler.executeQuery("INSERT INTO input_validation(id, id_input, validation_type)\
VALUES\
(9, 2, 'MAXCHAR'),\
(10, 5, 'MANDATORY');", (err: Error, result?: QueryResult) => {
(13, 2, 'MAXCHAR'),\
(14, 5, 'MANDATORY');", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("INSERT");
expect(result.rowCount).to.be.equal(2);
......@@ -155,14 +155,14 @@ describe("Database Handler", () => {
dbhandler.executeQuery("SELECT * FROM input_validation;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("SELECT");
expect(result.rowCount).to.be.equal(10);
expect(result.rowCount).to.be.equal(14);
done();
});
});
it("should remove non existent input validations", (done) => {
dbhandler.executeQuery("DELETE FROM input_validation WHERE id=11;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM input_validation WHERE id=15;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(0);
......@@ -172,7 +172,7 @@ describe("Database Handler", () => {
it("should remove existent input validations", (done) => {
dbhandler.executeQuery("DELETE FROM input_validation WHERE id=9 OR id=10;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM input_validation WHERE id=13 OR id=14;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(2);
......@@ -183,8 +183,8 @@ describe("Database Handler", () => {
it("should insert a input validations arguments", (done) => {
dbhandler.executeQuery("INSERT INTO input_validation_argument(id, id_input_validation, placement, argument)\
VALUES\
(5, 1, 2, '10'),\
(6, 2, 2, '2');", (err: Error, result?: QueryResult) => {
(8, 1, 2, '10'),\
(9, 2, 2, '2');", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("INSERT");
expect(result.rowCount).to.be.equal(2);
......@@ -197,7 +197,7 @@ describe("Database Handler", () => {
dbhandler.executeQuery("SELECT * FROM input_validation_argument;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("SELECT");
expect(result.rowCount).to.be.equal(6);
expect(result.rowCount).to.be.equal(9);
done();
});
});
......@@ -212,7 +212,7 @@ describe("Database Handler", () => {
});
it("should remove existent input validations arguments", (done) => {
dbhandler.executeQuery("DELETE FROM input_validation_argument WHERE id=5 OR id=6;", (err: Error, result?: QueryResult) => {
dbhandler.executeQuery("DELETE FROM input_validation_argument WHERE id=8 OR id=9;", (err: Error, result?: QueryResult) => {
expect(err).to.be.a("null");
expect(result.command).to.be.equal("DELETE");
expect(result.rowCount).to.be.equal(2);
......@@ -305,7 +305,7 @@ describe("Read and Write on Database", () => {
it("should list all forms", (done) => {
dbhandler.listForms((err: Error, forms?: Form[]) => {
expect(err).to.be.a("null");
expect(forms.length).to.be.equal(3);
expect(forms.length).to.be.equal(4);
for (let i = 0; i < forms.length; i++){
expect(forms[i].id).to.be.equal(i + 1);
expect(forms[i].title).to.be.equal("Form Title " + (i + 1));
......@@ -411,8 +411,8 @@ describe("Read and Write on Database", () => {
const form = new Form(OptHandler.form(formObj));
dbhandler.writeForm(form, (err: Error, formResult: Form) => {
expect(err).to.be.a("null");
expect(formResult.id).to.be.equal(4);
let inputId: number = 8;
expect(formResult.id).to.be.equal(5);
let inputId: number = 11;
for (const input of formResult.inputs){
expect(input.id).to.be.equal(inputId);
inputId++;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment