Users

We will start the users microservice test coverage. Create a users.rs file and import the created modules into it with the necessary types:

mod types;
mod utils;

use self::types::UserId;
use self::utils::{Method, WebApi};

At first, we have to check that the microservice is alive. Add the users_healthcheck method:

#[test]
fn users_healthcheck() {
let mut api = WebApi::users();
api.healthcheck("/", "Users Microservice");
}

It creates an instance of the WebApi struct using the users method that already configures it for interaction with the users microservice. We use the healthcheck method to check the root path of a service that has to return the "Users Microservice" string.

The main purpose of the users microservice is a new users' registration, and the authorization of registered users. Create a check_signup_and_signin function that will generate a new user, register it by sending a request to the /signup path, and then try to log in using the /signin path:

#[test]
fn check_signup_and_signin() {
let mut api = WebApi::users();
let username = utils::rand_str() + "@example.com";
let password = utils::rand_str();
let params = vec![
("email", username.as_ref()),
("password", password.as_ref()),
];
let _: () = api.request(Method::POST, "/signup", params);

let params = vec![
("email", username.as_ref()),
("password", password.as_ref()),
];
let _: UserId = api.request(Method::POST, "/signin", params);
}

We created a new WebApi instance that has targeted to our users microservice. The values of the username and password are generated by the rand_str function call of the utils module that we created earlier. After this, we prepare parameters to emulate sending an HTML form to a server with a POST request. The first request registers a new user; the second request tries to authorize it with the same form parameters.

Since the users microservice is used internally by the router microservice, it returns a raw UserId struct. We will parse it, but won't use it, because we have already checked that the microservice works, as it won't return users' IDs for bad credentials.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.136