Commit dbfbfd23 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴 Committed by Lucas Fernandes de Oliveira

issue #9: Create route to get form

Signed-off-by: Matheus Horstmann's avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent c4ee4d83
......@@ -4,6 +4,9 @@ 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.9 - 10-04-2019
### Added
- Method read in Form controller to get a Form
## 0.0.8 - 10-04-2019
### Changed
- main.ts to remove more dummie class
......
{
"name": "form-creator-api",
"version": "0.0.7",
"version": "0.0.9",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
......@@ -22,6 +22,8 @@
import * as request from "supertest";
import { expect } from "chai";
import * as server from "../../main";
import { InputType, ValidationType } from "../../utils/enumHandler";
describe("API data controller", () => {
......@@ -48,4 +50,64 @@
.end(done);
});
it("should respond 200 when getting valid form", (done) => {
request(server)
.get("/forms/1")
.expect(200)
.expect((res: any) => {
expect(res.body).to.be.an("object");
expect(res.body.id).to.be.equal(1);
expect(res.body.title).to.be.equal("Form Title 1");
expect(res.body.description).to.be.equal("Form Description 1");
expect(res.body.inputs.length).to.be.equal(3);
expect(res.body.inputs[0].id).to.be.equal(1);
expect(res.body.inputs[0].placement).to.be.equal(0);
expect(res.body.inputs[0].description).to.be.equal("Description Question 1 Form 1");
expect(res.body.inputs[0].question).to.be.equal("Question 1 Form 1");
expect(res.body.inputs[0].type).to.be.equal(InputType.TEXT);
expect(res.body.inputs[0].validation.length).to.be.equal(0);
expect(res.body.inputs[1].id).to.be.equal(3);
expect(res.body.inputs[1].placement).to.be.equal(1);
expect(res.body.inputs[1].description).to.be.equal("Description Question 2 Form 1");
expect(res.body.inputs[1].question).to.be.equal("Question 2 Form 1");
expect(res.body.inputs[1].type).to.be.equal(InputType.TEXT);
expect(res.body.inputs[1].validation.length).to.be.equal(2);
expect(res.body.inputs[1].validation[0].type).to.be.equal(ValidationType.MAXCHAR);
expect(res.body.inputs[1].validation[0].arguments.length).to.be.equal(1);
expect(res.body.inputs[1].validation[0].arguments[0]).to.be.equal("10");
expect(res.body.inputs[1].validation[1].type).to.be.equal(ValidationType.MINCHAR);
expect(res.body.inputs[1].validation[1].arguments.length).to.be.equal(1);
expect(res.body.inputs[1].validation[1].arguments[0]).to.be.equal("2");
expect(res.body.inputs[2].id).to.be.equal(2);
expect(res.body.inputs[2].placement).to.be.equal(2);
expect(res.body.inputs[2].description).to.be.equal("Description Question 3 Form 1");
expect(res.body.inputs[2].question).to.be.equal("Question 3 Form 1");
expect(res.body.inputs[2].type).to.be.equal(InputType.TEXT);
expect(res.body.inputs[2].validation.length).to.be.equal(2);
expect(res.body.inputs[2].validation[0].type).to.be.equal(ValidationType.REGEX);
expect(res.body.inputs[2].validation[0].arguments.length).to.be.equal(1);
expect(res.body.inputs[2].validation[0].arguments[0]).to.be.equal("\\d{5}-\\d{3}");
expect(res.body.inputs[2].validation[1].type).to.be.equal(ValidationType.MANDATORY);
expect(res.body.inputs[2].validation[1].arguments.length).to.be.equal(0);
})
.end(done);
});
it("should respond 500 when getting inexistent item", (done) => {
request(server)
.get("/forms/10")
.expect(500)
.expect((res: any) => {
const message = "Form with id: '10' not found. 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);
});
});
......@@ -49,4 +49,21 @@ export class FormCtrl {
}
public static read(req: Request, res: Response, next: NextFunction) {
req.db.readForm(req.params.id, (err: Error, form?: Form) => {
if (err){
res.status(500).json({
message: "Form with id: '" + req.params.id + "' not found. Some error has occurred. Check error property for details.",
error: err
});
return;
}
if (form) {
res.json(form);
return;
}
});
}
}
......@@ -48,6 +48,7 @@ app.use("/", DbHandlerMw());
// Setup routes
app.get("/forms/", FormCtrl.list);
app.get("/forms/:id", FormCtrl.read);
// Listening
......
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