fleet/frontend/test/mocks
Gabe Hernandez d0ded91d0b partial implementation of user table with generic table and new create user form (#500)
* use new data table in user manage page'

* remove default empty array hiddenColumns props, was causing render performance problems

* remove unused tooltip in hostcontainer

* add search to user manage table

* add query params to user GET requests

* move createUserForm closer to user management page

* starting to implement create user modal

* starting to add team checking functionality to create user

* styling of select team form

* changing logic for selectedTeamsForm, simplifying

* updated SelectedTeamsForm to handle own state and pass back relevant state to parent

* created reusable infobanner component and use it in osquery options page

* use infobanner in createuserform

* create new Radio component and use in createuserform

* create new Radio component and use in createuserform

* added new radio buttons to createUserForm

* finish custom radio button styling

* finish styling of radio in createUserForm

* fix and add entities/users#loadAll tests

* remove unneeded tests and updated broken ones on UserManagementPage

* remove unused modules
2021-03-31 11:58:29 -07:00
..
account_mocks.js Refactor API client (#1335) 2017-03-02 17:07:01 -05:00
config_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
create_request_mock.js Refactor API client (#1335) 2017-03-02 17:07:01 -05:00
host_mocks.js Feature - add search and sort to host table (#341) 2021-02-25 12:05:08 +00:00
index.js Remove decorators and osquery config from the UI (#1769) 2018-05-08 11:03:32 -06:00
invite_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
label_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
pack_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
query_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
README.md Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
scheduled_query_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
session_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
status_label_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
target_mocks.js Deprecate /api/v1/kolide routes (#297) 2021-02-10 12:13:11 -08:00
user_mocks.js partial implementation of user table with generic table and new create user form (#500) 2021-03-31 11:58:29 -07:00

Kolide Request Mocks

Request mocks are used to intercept API requests when running tests. Requests are mocked to simulate valid and invalid requests. The naming convention is similar to the API client entity CRUD methods.

Using Mocks

// import the mocks you want in the test file
import queryMocks from 'test/mocks/query_mocks';

// mock the API request before making the API call
queryMocks.load.valid(bearerToken, queryID); // valid request
queryMocks.load.invalid(bearerToken, queryID); // invalid request

Each entity with mocked requests has a dedicated file in this directory containing the mocks for the entity, such as queryMocks in the example above. If requests need to be mocked for multiple entities, consider importing all mocks:

import mocks from 'test/mocks';

mocks.queries.load.valid(bearerToken, queryID);
mocks.packs.create.valid(bearerToken, params);

Creating Mocks

Mocks are created using the createRequestMock function.

The createRequestMock function returns a mocked request using the nock npm package.

Example:

// in /frontend/test/mocks/query_mocks.js
import createRequestMock from 'test/mocks/create_request_mock';
import { queryStub } from 'test/stubs';

const queryMocks = {
  load: {
    valid: (bearerToken, queryID) => {
      return createRequestMock({
        bearerToken,
        endpoint: `/api/v1/fleet/queries/${queryID}`,
        method: 'get',
        response: { query: { ...queryStub, id: queryID } },
        responseStatus: 200,
      });
    },
  },
}

export default queryMocks;

createRequestMock takes an options hash with the following options:

bearerToken

  • Type: String
  • Required?: False
  • Default: None
  • Purpose: Specifying the bearer token sets the Authorization header of the request and is often used when mocking authorized requests to the API.

endpoint

  • Type: String
  • Required?: True
  • Default: None
  • Purpose: The required endpoint option is the relative pathname of the request.

method

  • Type: String (get | post | patch | delete)
  • Required?: True
  • Default: None
  • Purpose: This string is the lower-cased request method. Options are get, post, patch, and delete.

params

  • Type: Object
  • Required?: False
  • Default: None
  • Purpose: This JS Object is for the parameters sent with a request. If the parameters are URL parameters, such as in a GET request, add the parameters to the endpoint option.

response

  • Type: Object
  • Required?: True
  • Default: None
  • Purpose: This JS Object represents the response from the API

responseStatus

  • Type: Number
  • Required?: False
  • Default: 200
  • Purpose: This value is used for the response status of the API call.

Examples

API Request

  • The mocked request is saved as a variable in order to assert that the request is made

Component Test

  • The request is not saved but we want to prevent attempting to make an API request.
  • There is no API to hit in tests so attempting to make an API call with result in warnings in the test output.