Restful Api

Purpose

  • We can run restful api tests through csv files

  • A single row in csv will have request, response and validation

  • This allows for writing large number of tests quickly covering the requirements

Implementation

  • We can set the values in the csv file as follows:

apiTestData/testCases/TestTestCases_database.csv

TestSuite

TestCaseID

RunFlag

Description

InterfaceType

UriPath

ContentType

Method

Option

RequestHeader

TemplateFile

RequestBody

OutputParam

RespCodeExp

ExpectedResponse

TcComments

TsUser

getAdminToken

Y

Retrieve a token from Token GeneratorSQLDB

RESTfulAPI

/auth/local

application/json

Post

{

"identifier": "<@adminUserName>",

"password": "<@adminUserPassword>"

}

user.role.id:<$roles>; jwt:<$accessTokenAdmin>;

user.id:<$userId>

200

{

"user": {

"username": "<@adminUserName>",

"email": "autouser313@gmail.com",

"provider": "local",

"confirmed": true,

"blocked": null

}

}

&&

_VERIFY_JSON_PART_

"user.username":1: hasItems("<@adminUserName>");

"user.email":1:: equalTo("autouser313@gmail.com");

"user.provider":1: isNotEmpty;

"user.role.name":1: contains("Administrator")

&&

_NOT_EMPTY_

TsUser

createUser

Y

create user

RESTfulAPI

/content-manager/explorer/user/?source=users-permissions

application/x-www-form-urlencoded

Post

Authorization: Bearer <@accessTokenAdmin>

{

"username":"zzz_test<@_TIME16>",

"email":"testuser+<@_TIME16>@gmail.com",

"password":"password<@_TIME16>",

"confirmed":true

}

id:<$userId>

201

{

"provider": "local",

"blocked": null

}

&&

_VERIFY_JSON_PART_

"id": isNotEmpty

Column Values

  • TestSuite: The name of the test series

  • TestCaseID: Unique test id for each test

  • RunFlag: set 'Y' to run the test. Set 'N' to skip.

  • Description: Short description of the test.

  • InterfaceType: RESTfulAPI

  • UriPath:

    • URI path append to the base URI defined in apiConfig.property

    • Full URL path can be used. If value starts with "http", then the url will not be appended to the base URI. eg. http://www.site.com/api/login

  • ContentType: The content type of the request

  • Method: type of call made: POST, GET, PUT, DELETE

  • Option:

  • RequestHeaders: The header values, separated by ";".

    • eg. Authorization: Bearer <@accessTokenAdmin>

  • TemplateFile:

  • RequestBody: The request body goes here. We can use values set in api config file through the syntax: <@variable>. eg. <@username>, where username="admin" defined in apiConfig.property file.

  • OutputParams: We can store response values into variables defined here. The variables will then be available for other tests.

    • Syntax: <$variable>. eg. NAME:1:<$name>. the variable "name" can then be access through syntax <@name> in subsequent tests

  • RespCodeExp: expected response code value from the api call

  • ExpectedResponse: Verification of the response goes here. More description in the interface sections.

  • TcComment: comment for the tests. eg. disable for such and such reasons.

Json Body Verification

  • We can verify json body and structure through "lenient" verification

  • {
    "user": {
    "username": "<@adminUserName>",
    "email": "autouser313@gmail.com",
    "provider": "local",
    "confirmed": true,
    "blocked": null
    }
    }
  • We are using json body to verify the response

  • The json body is strictly enforced, however, the variables are optional

    • eg. we're not including fields such as "date"

  • We can use variables as part of response

  • "username": "<@adminUserName>",
  • Json body verification and Json Path verification are sepearted by "&&"

    • {
      "user": {
      "username": "<@adminUserName>",
      "email": "autouser313@gmail.com",
      "provider": "local",
      "confirmed": true,
      "blocked": null
      }
      }
      &&
      _VERIFY_JSON_PART_
      "user.username": hasItems("<@adminUserName>");

Jason Path Verification

  • Verification are separated by ";"

Json Path Equals

"user.email":1: equalTo("autouser313@gmail.com");
  • first instance of user.email equals "autouser313@gmail.com"

All Values In Json Path Equals

"user.email": equalTo("autouser313@gmail.com");
  • All user.email values will be stored in an array, and compared to the value

  • eg. if we have response of 2 users:

  • {
    "user": {
    "username": user1,
    "email": "autouser1@gmail.com",
    "provider": "local",
    "confirmed": true,
    "blocked": null
    }
    "user": {
    "username": user2,
    "email": "autouser2@gmail.com",
    "provider": "local",
    "confirmed": true,
    "blocked": null
    }
    }
  • Then are verification becomes:

  • "user.email": equalTo("autouser1@gmail.com","autouser2@gmail.com");

Jason Path Contains

NAME:1: contains(Paul);
  • Column "NAME" at row 1 contains the text: Paul

All Values in Json Path Contain

NAME:contains(Paul,Allen,Teddy,Mark);
  • We have 4 or more rows with column "Name" which contain: Paul, Allen, Teddy, Mark

Json Path Not Empty

ADDRESS:1:isNotEmpty;
  • Column "Address" at row 1 is not empty

  • Usefully if we want to check a value exists but don't want to verify the value. eg. time stamp