Giter VIP home page Giter VIP logo

endpoint-testing-afternoon's Introduction

Project Summary

In this project, we will practice the basics of endpoint testing in Postman. Using a provided Postman collection, we'll create tests for a server's endpoints.

If you need to refresh on Postman's documentation: Click Me!

Setup

  • fork and clone this repository.
  • cd into the root of the project.
  • Run npm install.
  • Run nodemon to start up the server.
    • The server will run on port 3535, do not change this port.

Step 1

Summary

In this step, we'll import the Postman collection into Postman.

Instructions

  • Open Postman.
  • Click on the import button located in the top left corner of Postman.
    • The file you are importing is inside of the postman_collection folder in this repo.
  • After importing, you should have a collection called Endpoint Testing Afternoon.

Solution

Step 2

Summary

In this step, we will create a Postman test for fetching all users.

Instructions

  • Click on the GET - All Users request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status code is 200.
  • Create a test to verify the returned data is an Array.
  • Create a test to verify the returned data has a length of 100.

Solution

GET - All Users
const responseJSON = pm.response.json();

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});

pm.test("Returned data is an array", function () {
  pm.expect( Array.isArray( responseJSON ) ).to.eql( true );
});

pm.test("Returned data has a length of 100", function () {
  pm.expect( responseJSON.length ).to.eql( 100 ); 
});

Step 3

Summary

In this step, we will create a Postman test for fetching users by ID.

Instructions

  • Click on the GET - User by ID request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status is 200.
  • Create a test to verify the returned data is an Array with a length of 1.
  • Create a test to verify the returned data has an object with the following properties:
    • id equal to 9.
    • first_name equal to "Tatum".
    • last_name equal to "Vell".
    • email equal to "[email protected]"
    • city equal to "Youngstown".
    • state equal to "Ohio".
    • phone equal to "(330) 6802507"

Solution

GET - User by ID
const expectedObject = {
  id: 9,
  first_name: "Tatum",
  last_name: "Vell",
  email: "[email protected]",
  city: "Youngstown",
  state: "Ohio",
  phone: "(330) 6802507"
};

const responseJSON = pm.response.json();

pm.test("Status code is 200", function () {
  pm.response.to.have.status( 200 );
});

pm.test("Returned data is an Array with length of 1", function () {
  pm.expect( Array.isArray( responseJSON ) ).to.eql( true );
  pm.expect( responseJSON.length ).to.eql( 1 );
});

pm.test("Returned data is expected", function () {
  pm.expect( responseJSON[0] ).to.eql( expectedObject ); 
});

Step 4

Summary

In this step, we will create a Postman test for fetching a user by ID that returns an error.

Instructions

  • Click on the GET - User by ID ( error ) request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status is 400.
  • Create a test to verify the returned message is "User id sent must be a number".

Solution

GET - User by ID ( error )
pm.test("Status code is 400", function () {
  pm.response.to.have.status( 400 );
});

pm.test("Returned error message is expected", function () {
  pm.expect( pm.response.text() ).to.eql("User id sent must be a number");
});

Step 5

Summary

In this step, we will create a Postman test for fetching users with a query.

Instructions

  • Click on the GET - User with Query request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status is 200.
  • Create a test to verify the return data set has a length greater than 0.

Solution

GET - User with Query
const responseJSON = pm.response.json();

pm.test("Status code is 200", function () {
  pm.response.to.have.status( 200 );
});

pm.test("Return data has a length greator than 0", function () {
  pm.expect( responseJSON.length > 0 ).to.eql( true );
});

Step 6

Summary

In this step, we will create a Postman test for fetching users with a query that returns an error.

Instructions

  • Click on the GET - User with Query ( error ) request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status is 400.
  • Create a test to verify the returned message is "Improper query sent in request: citty=new york".

Solution

GET - User with Query ( error )
pm.test("Status code is 400", function () {
  pm.response.to.have.status( 400 );
});

pm.test("Returned error message is expected", function () {
  pm.expect( pm.response.text() ).to.eql("Improper query sent in request: citty=new york");
});

Step 7

Summary

In this step, we will create a Postman test for updating a user by ID.

Instructions

  • Click on the PUT - Update user by ID request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status is 200.
  • Create a test to verify the returned data is an Array with a length of 1.
  • Create a test to verify the returned user has an updated object with the following properties:

Solution

PUT - Update user by ID
const responseJSON = pm.response.json();

pm.test("Status code is 200", function () {
  pm.response.to.have.status( 200 );
});

pm.test("Returned data is an Array with a length of 1", function () {
  pm.expect( Array.isArray( responseJSON ) ).to.eql( true );
  pm.expect( responseJSON.length ).to.eql( 1 );
});

const user = responseJSON[0];

pm.test("Returned email is '[email protected]'", function () {
  pm.expect( user.email ).to.eql( "[email protected]" );
});

pm.test("Returned city is 'Pittsburg'", function () {
  pm.expect( user.city ).to.eql( "Pittsburg" );
});

Step 8

Summary

In this step, we will create a Postman test for updating a user by ID that returns an error.

Instructions

  • Click on the PUT - Update User by ID ( error ) request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned stats is 400.
  • Create a test to verify the returned message is "".

Solution

PUT - Update User by ID ( error )
pm.test("Status code is 400", function () {
  pm.response.to.have.status( 400 );
});

pm.test("Returned error message is expected", function () {
  pm.expect( pm.response.text() ).to.eql("Error with user ID in request.");
});

Step 9

Summary

In this step, we will create a Postman test for creating a new user.

Instructions

  • Click on the POST - Create user request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status code is 200.
  • Create a test to verify the returned data is an Array with a length of 1.
  • Create a test to verify the returned user has the following data:
    • first_name equals "Bruce".
    • last_name equals "Wayne".
    • email equals "[email protected]".
    • city equals "Gotham".
    • state equals "New Jersey".
    • phone equals "(856) 6044252".
  • Create a test to verify the returned users has an id property that equals a number.

Solution

POST - Create user
const responseJSON = pm.response.json();

const user = responseJSON[0];

const expectedUser = {
  id: user.id,
  first_name: "Bruce",
  last_name: "Wayne",
  email: "[email protected]",
  city: "Gotham",
  state: "New Jersey",
  phone: "(856) 6044252"
};

pm.test("Status code is 200", function () {
  pm.response.to.have.status( 200 ); 
});

pm.test("Returned data is an Array with a length of 1", function () {
  pm.expect( Array.isArray( responseJSON ) ).to.eql( true );
  pm.expect( responseJSON.length ).to.eql( 1 );
});

pm.test("Returned user is expected", function () {
  pm.expect( user ).to.eql( expectedUser );
});

pm.test("Returned user id is a number", function () {
  pm.expect( typeof( user.id ) ).to.eql('number'); 
});

Step 10

Summary

In this step, we will create a Postman test for creating a user that returns an error.

Instructions

  • Click on the POST - Create user ( error ) request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status code is 200.
  • Create a test to verify the returned message is "All needed user info was not sent in the body of request.".

Solution

POST - Create user ( error )
pm.test("Status code is 400", function () {
  pm.response.to.have.status( 400 );
});

pm.test("Returned error message is expected", function () {
  pm.expect( pm.response.text() ).to.eql("All needed user info was not sent in the body of request.");
});

Step 11

Summary

In this step, we will create a Postman test for removing a user by ID.

Instructions

  • Click on the DELETE - Remove user request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status code is 200.
  • Create a test to verify the returned user's id is equal to 66.

Solution

DELETE - Remove user
const user = pm.response.json()[0];

pm.test("Status code is 200", function () {
  pm.response.to.have.status( 200 );
});

pm.test("Returned user ID is equal to 66", function () {
  pm.expect( user.id ).to.eql( 66 );
});

Step 12

Summary

In this step, we will create a Postman test for removing a user that returns an error.

Instructions

  • Click on the DELETE - Remove user ( error ) request.
  • Click on the Send button to see the returned data.
  • Create a test to verify the returned status code is 404.
  • Create a test to verify the returned message is "No user with an ID of 508.".

Solution

DELETE - Remove user ( error )
pm.test("Status code is 404", function () {
  pm.response.to.have.status( 404 );
});

pm.test("Returned error message is expected", function () {
  pm.expect( pm.response.text() ).to.eql("No user with an ID of 508.");
});

Step 13

Summary

In this step, we'll restart the node server and run the Postman collection of tests as a whole.

Instructions

  • Restart the sever.
  • Click on the right arrow next to the collection name.
  • Click the Run button.
  • Select the correct collection from the list on the left.
  • Click the blue button in the bottom at the bottom of the left side-menu.

Solution

Contributions

If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.

Copyright

© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.

endpoint-testing-afternoon's People

Contributors

devlemire avatar joeblank avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.