Advertisement
Matteogoli

testing node.js

Nov 28th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const request = require('supertest');
  2. const url = 'http://localhost:3000/submissions';
  3.  
  4. let submission = undefined;
  5.  
  6. // CREATE
  7. test('POST /submissions => return 201 Submission created', async () => {
  8.     const response = await request(url).post('/').send({
  9.         taskId: '1',
  10.         userId: '2',
  11.         answer: 'Answer'
  12.     });
  13.  
  14.     // Save the object created
  15.     submission = response.body.submission;
  16.    
  17.     // Check the status code
  18.     expect(response.statusCode).toBe(201);
  19.    
  20.     // Check the correctness of the fields
  21.     expect(submission.taskId).toBe('1');
  22.     expect(submission.userId).toBe('2');
  23.     expect(submission.answer).toBe('Answer');
  24. });
  25.  
  26. // UPDATE
  27. test('PUT /submissions => return 200 Updated', async () => {
  28.     let url_id = '/' + submission.id;
  29.     const response = await request(url).put(url_id).send({
  30.         answer: 'New answer'
  31.     });
  32.  
  33.     // Save the object created
  34.     submission = response.body.submission;
  35.    
  36.     // Check the status code
  37.     expect(response.statusCode).toBe(200);
  38.    
  39.     // Check the correctness of the updated field
  40.     expect(submission.answer).toBe('New answer');
  41. });
  42.  
  43. // GET ID
  44. test('GET /submissions/id => return 200 and a Submission object', async () => {
  45.     let url_id = '/' + submission.id;
  46.     const response = await request(url).get(url_id);
  47.    
  48.     // Check the status code
  49.     expect(response.statusCode).toBe(200);
  50.    
  51.     // Check if the returned object is equals to the local object
  52.     expect(response.body.submission).toEqual(submission);
  53. });
  54.  
  55. // GET ALL
  56. test('GET /submissions => return 200 and a Submission object', async () => {
  57.     const response = await request(url).get('/');
  58.  
  59.     // Check the status code
  60.     expect(response.statusCode).toBe(200);
  61.    
  62.     // TODO: find better tests
  63.     // Check if the list contains at least one object
  64.     expect(response.body.length).toBeGreaterThan(0);
  65. });
  66.  
  67. // DELETE
  68. test('DELETE /submissions/id => return 200 OK', async () => {
  69.     let url_id = '/' + submission.id;
  70.     const response = await request(url).delete(url_id);
  71.  
  72.     // Check the status code
  73.     expect(response.statusCode).toBe(200);
  74. });
  75.  
  76. // GET ID - NOT FOUND
  77. test('GET /submissions/id with the id of a deleted submission => return 404 Not found', async () => {
  78.     let url_id = '/' + submission.id;
  79.     const response = await request(url).get(url_id);
  80.    
  81.     // Check the status code
  82.     expect(response.statusCode).toBe(404);
  83. });
  84.  
  85. // UPDATE - NOT FOUND
  86. test('PUT /submissions/id with the id of a deleted submission => return 404 Not found', async () => {
  87.     const response = await request(url).put('/a').send({
  88.         answer: 'New answer'
  89.     });
  90.  
  91.     // Check the status code
  92.     expect(response.statusCode).toBe(404);
  93. });
  94.  
  95. // DELETE - NOT FOUND
  96. test('DELETE /submissions/id with the id of a deleted submission => return 404 Not found', async () => {
  97.     let url_id = '/' + submission.id;
  98.     const response = await request(url).delete(url_id);
  99.  
  100.     // Check the status code
  101.     expect(response.statusCode).toBe(404);
  102. });
  103.  
  104. // CREATE - BAD REQUEST
  105. test('POST /submissions with one missing parameter => return 400 Bad request', async () => {
  106.     const response = await request(url).post('/').send({
  107.         // missing taskId: '1',
  108.         userId: '2',
  109.         answer: 'Answer'
  110.     });
  111.  
  112.     // Check the status code
  113.     expect(response.statusCode).toBe(400);
  114. });
  115.  
  116. // TODO: serve sta roba? PerchΓ© se li lancio ricevo un 404 e non un 400???
  117. /*
  118. // UPDATE - BAD REQUEST
  119. test('PUT /submissions/id with no id => return 400 Bad request', async () => {
  120.     const response = await request(url).put('/').send({
  121.         answer: 'New answer'
  122.     });
  123.  
  124.     // Check the status code
  125.     expect(response.statusCode).toBe(400);
  126. });
  127.  
  128. // DELETE - BAD REQUEST
  129. test('DELETE /submissions/id with no id => return 400 Bad request', async () => {
  130.     const response = await request(url).delete('/');
  131.  
  132.     // Check the status code
  133.     expect(response.statusCode).toBe(400);
  134. });
  135. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement