|
| 1 | +# ApiDataFactory |
| 2 | + |
| 3 | +Helper for managing remote data using REST API. |
| 4 | +Uses data generators like [rosie](https://github.com/rosiejs/rosie) or factory girl to create new record. |
| 5 | + |
| 6 | +By defining a factory you set the rules of how data is generated. |
| 7 | +This data will be saved on server via REST API and deleted in the end of a test. |
| 8 | + |
| 9 | +### Use Case |
| 10 | + |
| 11 | +Acceptance tests interact with a websites using UI and real browser. |
| 12 | +There is no way to create data for a specific test other than from user interface. |
| 13 | +That makes tests slow and fragile. Instead of testing a single feature you need to follow all creation/removal process. |
| 14 | + |
| 15 | +This helper solves this problem. |
| 16 | +Most of web application have API, and it can be used to create and delete test records. |
| 17 | +By combining REST API with Factories you can easily create records for tests: |
| 18 | + |
| 19 | +```js |
| 20 | +I. have( 'user', { login : 'davert', email : '[email protected]' }); |
| 21 | +let id = yield I.have('post', { title: 'My first post'}); |
| 22 | +I.haveMultiple('comment', 3, {post_id: id}); |
| 23 | +``` |
| 24 | + |
| 25 | +To make this work you need |
| 26 | + |
| 27 | +1. REST API endpoint which allows to perform create / delete requests and |
| 28 | +2. define data generation rules |
| 29 | + |
| 30 | +### Setup |
| 31 | + |
| 32 | +Install [Rosie](https://github.com/rosiejs/rosie) and [Faker](https://www.npmjs.com/package/faker) libraries. |
| 33 | + |
| 34 | + npm i rosie faker --save-dev` |
| 35 | + |
| 36 | +Create a factory file for a resource. |
| 37 | + |
| 38 | +See the example for Posts factories: |
| 39 | + |
| 40 | +```js |
| 41 | +// tests/factories/posts.js |
| 42 | + |
| 43 | +var Factory = require('rosie').Factory; |
| 44 | +var faker = require('faker'); |
| 45 | + |
| 46 | +module.exports = new Factory() |
| 47 | + // no need to set id, it will be set by REST API |
| 48 | + .attr('author', () => faker.name.findName()) |
| 49 | + .attr('title', () => faker.lorem.sentence()) |
| 50 | + .attr('body', () => faker.lorem.paragraph()); |
| 51 | +``` |
| 52 | + |
| 53 | +For more options see [rosie documentation](https://github.com/rosiejs/rosie). |
| 54 | + |
| 55 | +Then configure ApiDataHelper to match factories and REST API: |
| 56 | + |
| 57 | +### Configuration |
| 58 | + |
| 59 | +ApiDataFactory has following config options: |
| 60 | + |
| 61 | +- `endpoint`: base URL for the API to send requests to. |
| 62 | +- `cleanup`: should inserted records be deleted up after tests. Default: true |
| 63 | +- `factories`: list of defined factories |
| 64 | +- `REST`: configuration for REST requests |
| 65 | + |
| 66 | +See the example: |
| 67 | + |
| 68 | +```js |
| 69 | + "ApiDataFactory": { |
| 70 | + "endpoint": "http://user.com/api", |
| 71 | + "cleanup": true, |
| 72 | + "factories": { |
| 73 | + "post": { |
| 74 | + "uri": "/posts" |
| 75 | + "factory": "./factories/post" |
| 76 | + }, |
| 77 | + "comment": { |
| 78 | + "factory": "./factories/comment", |
| 79 | + "create": { "post": "/comments/create" }, |
| 80 | + "delete": { "post": "/comments/delete" } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +It is required to set REST API `endpoint` which is the baseUrl for all API requests. |
| 87 | +Factory file is expected to be passed via `factory` option. |
| 88 | + |
| 89 | +This Helper uses [REST](http://codecept.io/helpers/REST/) helper and accepts its configuration in "REST" section. |
| 90 | +So, in order to set default headers or timeout you should add: |
| 91 | + |
| 92 | +```js |
| 93 | +"ApiDataFactory": { |
| 94 | + "REST": { |
| 95 | + "timeout": "100000", |
| 96 | + "defaultHeaders": { |
| 97 | + "auth": "111111" |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Api Requests |
| 104 | + |
| 105 | +By default to create a record ApiDataFactory will use endpoint and plural factory name: |
| 106 | + |
| 107 | +- create: `POST {endpoint}/{resource} data` |
| 108 | +- delete: `DELETE {endpoint}/{resource}/id` |
| 109 | + |
| 110 | +Example (`endpoint`: `http://app.com/api`): |
| 111 | + |
| 112 | +- create: POST request to `http://app.com/api/users` |
| 113 | +- delete: DELETE request to `http://app.com/api/users/1` |
| 114 | + |
| 115 | +However this behavior can be configured with following options: |
| 116 | + |
| 117 | +- `uri`: set different resource uri. Example: `uri: account` => `http://app.com/api/account`. |
| 118 | +- `create`: override create options. Expected format: `{ method: uri }`. Example: `{ "post": "/users/create" }` |
| 119 | +- `delete`: override delete options. Expected format: `{ method: uri }`. Example: `{ "post": "/users/delete/{id}" }` |
| 120 | + |
| 121 | +**Parameters** |
| 122 | + |
| 123 | +- `config` |
| 124 | + |
| 125 | +## _fetchId |
| 126 | + |
| 127 | +Fetches id of a record after it was created by `have`. |
| 128 | +By default fetched `id` attribute from JSON body. |
| 129 | + |
| 130 | +Customize it from custom Helper file: |
| 131 | + |
| 132 | +```js |
| 133 | +this.helpers['ApiDataFactory']._fetchId = (body, factory) { |
| 134 | + return body[factory][0].id; |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +**Parameters** |
| 139 | + |
| 140 | +- `body` **Any** |
| 141 | +- `factory` **Any** |
| 142 | + |
| 143 | +## _requestCreate |
| 144 | + |
| 145 | +Executes request to create a record in API. |
| 146 | +Can be replaced from a in custom helper. |
| 147 | + |
| 148 | +**Parameters** |
| 149 | + |
| 150 | +- `factory` **Any** |
| 151 | +- `data` **Any** |
| 152 | + |
| 153 | +## _requestDelete |
| 154 | + |
| 155 | +Executes request to delete a record in API |
| 156 | +Can be replaced from a custom helper. |
| 157 | + |
| 158 | +**Parameters** |
| 159 | + |
| 160 | +- `factory` **Any** |
| 161 | +- `id` **Any** |
| 162 | + |
| 163 | +## have |
| 164 | + |
| 165 | +Generates a new record using factory and saves API request to store it. |
| 166 | + |
| 167 | +```js |
| 168 | +// create a user |
| 169 | +I.have('user'); |
| 170 | +// create user with defined email |
| 171 | +I. have( 'user', { email : '[email protected]'}); |
| 172 | +``` |
| 173 | + |
| 174 | +**Parameters** |
| 175 | + |
| 176 | +- `factory` **Any** factory to use |
| 177 | +- `params` **Any** predefined parameters |
| 178 | + |
| 179 | +## haveMultiple |
| 180 | + |
| 181 | +Generates bunch of records and saves multiple API requests to store them. |
| 182 | + |
| 183 | +```js |
| 184 | +// create 3 posts |
| 185 | +I.have('post', 3); |
| 186 | + |
| 187 | +// create 3 posts by one author |
| 188 | +I.have('post', 3, { author: 'davert' }); |
| 189 | +``` |
| 190 | + |
| 191 | +**Parameters** |
| 192 | + |
| 193 | +- `factory` **Any** |
| 194 | +- `times` **Any** |
| 195 | +- `params` **Any** |
0 commit comments