-
This unit test will check the login using username and password:
POST
call to endpoint/api/login/
with username and password in the body.- Send a request to the server and response would be recorded.
- Using assert.Equal will test if the response code is equal to http.StatusOK (i.e 200),that means test is OK.
- Unmarshal the json recieved in the response body and will check if there is no error using assert.NoError and using assert.True to check the validated field in the response body is true.
-
This unit test will check the login using bad username and password:
POST
call to endpoint/api/login/
with username and password {xxx} in the body.- Send a request to the server and response would be recorded.
- Using assert.Equal will test if the response code is equal to http.StatusUnauthorized (i.e 401),that means test is OK.
- Unmarshal the json recieved in the response body and will check if there is no error using assert.NoError and using assert.False to check the validated field in the response body is false.
-
This unit test will check the API that will create a new user:
- Creating user with payload (i.e _id: username , name: username , password : password , role: admin).
PUT
call to the endpoint/api/user
.- Using assert.Equal will test if the response code is equal to http.StatusOK (i.e 200),that means test is OK.
- Test new user login using the above username and password in body to the endpoint
/api/login
. - Response will be recorded and using assert.Equal will test if the response code is equal to http.StatusUnauthorized (i.e 401),that means test is OK.
- Unmarshal the json recieved in the response body and will check if there is no error using assert.NoError and using assert.True to check the validated field in the response body is true.
-
This unit test will update the existing user :
- Update the existing username by using paylod (i.e _id: username , name: username-updated , password : password , role: admin).
PUT
call to the endpoint/api/user
.- Using assert.Equal will test if the response code is equal to http.StatusOK (i.e 200),that means test is OK.
- Get the user name using auth.Getuser method and using assert.Equal to match that the user Name has been successfully updated.
-
This unit test will create user with error input:
PUT
call to the endpoint/api/user
withxxx
in body.- Request to the server and response will be recorded.
- Using assert.Equal will test if the response code is equal to http.StatusBadRequest (i.e 400),that means test is OK.
-
This unit test will delete the existing userid:
DELETE
call to the endpoint/api/user/username
with empty body.- Using assert.Equal will test if the response code is equal to http.StatusOK (i.e 200),that means test is OK and user is deleted.
- Using auth.Getuser method to check user existence and using assert.False to verify that the user doest not exists.
-
This unit test will test delete the user with no existing userid:
DELETE
call to the endpoint/api/user/userNotExist
with empty body.- Using assert.Equal will test if the response code is equal to http.StatusOK (i.e 200),that means test is OK ,will get a null response.
-
This unit test will enlist all the users:
GET
call to the endpoint/api/user
with empty body.- Using assert.Equal will test if the response code is equal to http.StatusOK (i.e 200),that means test is OK.
- Unmarshal the json recieved in the response body and will check if there is no error using assert.NoError and using assert.GreaterorEqual to check the length of data (i.e number of users) are greater than or equal to 1 , finally using assert.Equal to verify that the first user is admin.