This repository has been archived by the owner on Feb 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
609 lines (516 loc) · 21 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Documentation Index</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.simplex.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top ">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">Documentation</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="topNavigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="module-thorn-Agent.html">thorn~Agent</a></li><li><a href="module-thorn-Fixtures.html">thorn~Fixtures</a></li><li><a href="utils.html">utils</a></li>
</ul>
</li>
<li class="dropdown">
<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="module-thorn.html">thorn</a></li>
</ul>
</li>
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="module-thorn-UserAgent.html">thorn~UserAgent</a></li>
</ul>
</li>
</ul>
<div class="col-sm-3 col-md-3">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
<div class="input-group-btn">
<button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container" id="toc-content">
<div class="row">
<div class="col-md-8">
<div id="main">
<section class="readme-section">
<article><h1>Thorn</h1><p>Thorn is composed of a set of helper abstractions built to ease the process of
setting up a SugarCRM's REST API testing environment and interacting with it.</p>
<h2>Prerequisites</h2><p>You should be familiar with <a href="https://mochajs.org/">mocha</a> and <a href="https://developers.google.com/web/fundamentals/getting-started/primers/promises">JavaScript Promises</a>.</p>
<h2>Installation and Running</h2><p>Thorn tests are run using the Mocha test runner. To add Thorn as a dependency, run</p>
<pre class="prettyprint source lang-bash"><code>$ yarn add --dev '@sugarcrm/thorn'</code></pre><p>You will need to invoke Mocha in order to run Thorn. Check out
<a href="https://mochajs.org/#usage">Mocha's usage information</a>, <a href="https://github.com/disqus/grunt-mocha">grunt-mocha</a>, and
<a href="https://github.com/knpwrs/gulp-spawn-mocha">gulp-spawn-mocha</a> for information about instantiating Mocha.</p>
<h2>Setup</h2><pre class="prettyprint source lang-javascript"><code>const {Agent, Fixtures} = require('@sugarcrm/thorn');</code></pre><h2>Thorn.Fixtures</h2><p><code>Thorn.Fixtures</code> is an object that handles the setup and cleanup process for test sets. It provides methods for creating records, record-relationships, and deleting records in the database.</p>
<h3>Methods</h3><h4><strong><code>Fixtures.create(models, options)</code> => <code>{Promise}</code></strong></h4><p>Method to create records in the database.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>models</code></td>
<td style="text-align:left">{Object|Object[]}</td>
<td style="text-align:left">Object or object array that specifies the records to be created. See <a href="#model-structure">Model Structure</a> for details.</td>
</tr>
<tr>
<td><code>options</code></td>
<td style="text-align:left">{Object}</td>
<td style="text-align:left">Optional, <code>options.module</code> is the default <code>module</code> for objects in <code>models</code> that do not specify one.</td>
</tr>
</tbody>
</table>
<p><strong>Returns:</strong></p>
<table>
<thead>
<tr>
<th>Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{Promise}</td>
<td style="text-align:left">A <code>Promise</code> which resolves to a map of module names to records created by this method call.</td>
</tr>
</tbody>
</table>
<p><br/></p>
<p><strong>Example:</strong></p>
<pre class="prettyprint source lang-javascript"><code>let AccountsContacts = [
{
module: 'Accounts',
attributes: {
name: 'MyAccount'
}
},
{
module: 'Contacts',
attributes: {
first_name: 'FirstName'
}
}
];
let DashboardsOnly = [
{
attributes: {
name: 'MyDashboard'
}
}
];
let response = yield Fixtures.create(AccountsContacts);
console.log(response); // Map containing one account, and one contact
/*
{
Accounts: [
{
id: '12257c7c-bb40-11e6-afb2-a0937b020fc9',
name: 'MyAccount',
...
_module: 'Accounts'
}
],
Contacts: [
{
id: '11232c7c-bb40-11e6-bfb2-a0937b020sc9',
first_name: 'FirstName'
...
_module: 'Contacts'
}
]
}
*/
response = yield Fixtures.create(DashboardsOnly, {module: 'Dashboards'});
console.log(response);
/*
{
Dashboards: [
{
id: '11232c7c-bb40-11e6-bfb2-a0937b020sc9',
name: 'MyDashboard',
...
_module: 'Dashboards'
}
]
}
*/</code></pre><p><br/></p>
<h4><strong><code>Fixtures.link(left, linkName, right)</code> => <code>{Promise}</code></strong></h4><p>Method to link records in the database.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>left</code></td>
<td style="text-align:left">{Object}</td>
<td style="text-align:left">A record from the resolution of <code>Fixtures.create</code>.</td>
</tr>
<tr>
<td><code>linkName</code></td>
<td style="text-align:left">{string}</td>
<td style="text-align:left">Relationship's link name.</td>
</tr>
<tr>
<td><code>right</code></td>
<td style="text-align:left">{Object}</td>
<td style="text-align:left">A record from the resolution of <code>Fixtures.create</code>.</td>
</tr>
</tbody>
</table>
<p><strong>Returns:</strong></p>
<table>
<thead>
<tr>
<th>Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{Promise}</td>
<td style="text-align:left">A <code>Promise</code> which resolves to the body of the server response.</td>
</tr>
</tbody>
</table>
<p><br/></p>
<p><strong>Example:</strong></p>
<pre class="prettyprint source lang-javascript"><code>let Account = {
module: 'Accounts',
attributes: {
name: 'LinkedAccount'
}
};
let Contact = {
module: 'Contacts',
attributes: {
last_name: 'LinkedContact'
}
};
let cachedRecords = yield Fixtures.create([Account, Contact]);
console.log(cachedRecords); // Map containing one Account and one Contact
let response = yield Fixtures.link(cachedRecords.Accounts[0], 'contacts', cachedRecords.Contacts[0]);
// Server response containing the Accounts record and a `related_records` property,
// which contains the Contacts record.
console.log(response);
/*
{
record: {
id: '12257c7c-bb40-11e6-afb2-a0937b020fc9',
name: 'LinkedAccount',
...
_module: 'Accounts'
},
related_records: [
{
id: '12557c7c-bd40-11e6-afb2-a0345b020fc9',
last_name: 'LinkedContact',
...
_module: 'Contacts'
}
]
}
*/</code></pre><p><br/></p>
<h4><strong><code>Fixtures.cleanup()</code> => <code>{Promise}</code></strong></h4><p>Method to delete all records previously created through <code>Fixtures.create</code>.
After cleanup, <code>Fixtures.lookup</code> will throw an error unless more records are created
with <code>Fixtures.create</code>.</p>
<p><strong>Returns:</strong></p>
<table>
<thead>
<tr>
<th>Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{Promise}</td>
<td style="text-align:left">A <code>Promise</code> which resolves to the server response to the delete request(s).</td>
</tr>
</tbody>
</table>
<p><br/></p>
<h4><strong><code>Fixtures.lookup(module, properties)</code> => <code>{Object}</code></strong></h4><p>Method that looks through the created records and retrieves the first record
that matches module and the key-value pairs in properties.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>module</code></td>
<td style="text-align:left">{string}</td>
<td style="text-align:left">Module name of the record.</td>
</tr>
<tr>
<td><code>properties</code></td>
<td style="text-align:left">{Object}</td>
<td style="text-align:left">Object of key-value pairs the record should contain.</td>
</tr>
</tbody>
</table>
<p><strong>Returns:</strong></p>
<table>
<thead>
<tr>
<th>Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{Object}</td>
<td style="text-align:left">A single record object.</td>
</tr>
</tbody>
</table>
<p><br/></p>
<h3>Model Structure</h3><p>Models is an object or array of objects that specifies the records the tester
intends to create.
<strong>Properties of each model object:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th style="text-align:left">Type</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>module</code></td>
<td style="text-align:left">{string}</td>
<td style="text-align:left">Optional, module name of the record.</td>
</tr>
<tr>
<td><code>attributes</code></td>
<td style="text-align:left">{Object}</td>
<td style="text-align:left">Specific fields the record should have. Unspecified required field values are auto-generated.</td>
</tr>
</tbody>
</table>
<p><br/></p>
<h3>Tips</h3><ul>
<li><p><code>Fixtures</code> is designed to be a tool that facilitates the setup and cleanup of test cases. Its methods are <em>not</em> meant
to provide a means of testing the correctness of SugarCRM's record creation, linking, and deletion APIs. Such tests
should make use of the request methods of <code>UserAgent</code>.</p>
</li>
<li><p>The same model object cannot be used to create multiple records; this will lead to collisions in <code>Fixtures</code> internal
ways of storing records. To reuse the same model to create multiple records, all but the first model must be cloned (
e.g. using <code>_.clone</code>).</p>
</li>
<li><p>Linking records requires that the records have already been created in the database. To avoid exceptions, structure the
record creations such that dependencies are met before <code>Fixtures</code> tries to make links.</p>
</li>
</ul>
<h2>Thorn.Agent</h2><p>Thorn provides an Agent class that simulates a REST API user agent.
You should use this class for all of your Thorn tests except those which test the Users API directly (in which case you should use the <a href="https://dareid.github.io/chakram/jsdoc/module-chakram.html">chakram API</a> directly).</p>
<h3>Creating Agents</h3><p>An Agent corresponds directly to a user that exists in the SugarCRM instance you are testing. All users except for the admin user specified by the <code>THORN_ADMIN_USERNAME</code> environment variable <em>must</em> be created by <code>Fixtures.create</code> before you attempt to create a user agent for it. Please refer to <a href="#thorn::fixtures">Thorn Fixtures API</a> to create the needed users.</p>
<p>User agents are created with <code>Agent.as</code>:</p>
<pre class="prettyprint source lang-javascript"><code>// assuming a user named "John" already exists
let john = Agent.as('John');</code></pre><p><strong>This also logs the user in if they have not been logged in already.</strong></p>
<h4>Default admin user</h4><p>You can retrieve the user agent corresponding to the default admin user with <code>Agent.as(Agent.ADMIN)</code>.</p>
<p>The default admin user should <strong>not</strong> be accessed by name, nor be created with <code>Fixtures.create</code>.</p>
<pre class="prettyprint source lang-javascript"><code>let badAdmin = Agent.as('admin'); // Don't do this
let goodAdmin = Agent.as(Agent.ADMIN);</code></pre><p>This ensures that the admin user can be used even on Sugar instances where the admin username is not "admin".</p>
<h3>Making requests</h3><p>Agents can make HTTP GET, POST, PUT, and DELETE requests against any SugarCRM REST API endpoint. Each request method has a corresponding function, which makes the desired request and returns a JavaScript Promise which resolves to a <a href="https://dareid.github.io/chakram/jsdoc/global.html#ChakramResponse">Chakram response object</a> corresponding to the server's response to the request:</p>
<pre class="prettyprint source lang-javascript"><code>let response = yield john.get('Accounts');
console.log('%j', response.response.body);
// displays response body</code></pre><p>Required user refreshes are handled automatically by an agent. If a request fails with HTTP status 401, the agent's authentication is refreshed and the request is automatically retried. However, each request is only retried once to prevent infinite loops.</p>
<h3>Request Methods</h3><h4>get</h4><pre class="prettyprint source lang-javascript"><code>let response = yield john.get('Accounts');
console.log(response.response.body);
/*
{
next_offset: -1,
records: [
{
id: '12257c7c-bb40-11e6-afb2-a0999b020fc9',
name: 'Accounts.Samantha',
date_entered: '2016-12-05T15:10:53-08:00',
date_modified: '2016-12-05T15:10:53-08:00',
...
_module: 'Accounts'
},
...
]
*/</code></pre><h4>post</h4><pre class="prettyprint source lang-javascript"><code>let response = yield john.post('Accounts', {name: 'Smith'});
console.log(response.response.body);
/*
{
id: '10e42218-bb41-11e6-82c7-a0999b020fc9',
name: 'Smith',
date_entered: '2016-12-05T15:18:00-08:00',
date_modified: '2016-12-05T15:18:00-08:00',
...
}
*/</code></pre><h4>put</h4><pre class="prettyprint source lang-javascript"><code>// assuming "id" is the the ID of Smith
let response = yield john.put('Accounts/' + id, {industry: 'Not For Profit'});
console.log(response.response.body);
/*
{
id: '10e42218-bb41-11e6-82c7-a0999b020fc9',
name: 'Smith',
date_entered: '2016-12-05T15:54:26-08:00',
date_modified: '2016-12-05T15:54:27-08:00',
industry: 'Not For Profit',
...
}
*/</code></pre><h4>delete</h4><pre class="prettyprint source lang-javascript"><code>let response = yield john.delete('Accounts' + id);
console.log(response.response.body);
/*
{id: '10e42218-bb41-11e6-82c7-a0999b020fc9'}
*/</code></pre><h3>Request Arguments</h3><p>All request methods accept an endpoint and HTTP request parameters; <code>Agent.put</code>, <code>Agent.post</code>, and <code>Agent.delete</code> additionally accept a request body.</p>
<h5>Endpoints</h5><p>Endpoints are specified relative to <code>/rest/<version>/</code> (see <a href="#api-versioning">API versioning</a>), so example endpoints might be <code>'Accounts'</code>, <code>'Contacts/<contactId>'</code>, or <code>'Forecasts/<timePeriodId>/progressRep/<userId>'</code>.</p>
<h5>Request Bodies</h5><p>Request bodies must be JSON-serializable JavaScript objects. They are passed directly to the SugarCRM server.</p>
<h5>Parameters</h5><p>See <a href="https://github.com/request/request#requestoptions-callback">the parameter documentation</a> for details of possible parameters.</p>
<p>It is <em>NOT</em> necessary to explicitly include OAuth tokens or other authentication details in your requests; this is handled transparently by Thorn. <strong>Attempting to do so may interfere with Thorn's proper operation.</strong></p>
<h4>API versioning</h4><p>Agents make requests against the default API version (currently, <code>v10</code>) by default. To make requests against a different API version, use <code>Agent.on</code>:</p>
<pre class="prettyprint source lang-javascript"><code>let johnV11 = john.on('v11');
// all requests made against API version 11
let response = yield johnV11.get('Dashboards');
...
let response = yield johnV11.get('KBContents');
...</code></pre><p>Note that the original Agent remains valid and can continue to make requests against the default API version with no additional effort:</p>
<pre class="prettyprint source lang-javascript"><code>let response = yield john.get('Notifications')
...</code></pre><h2>Best Practices</h2><ul>
<li><p>Every test file should be wrapped in a <code>describe</code> block to protect against cross-test contamination.</p>
</li>
<li><p>In the <code>before</code> and <code>after</code> functions, as well as in <code>Promise</code> chains, every <code>Promise</code> created must be <code>returned</code> in
order for server requests to effectuate. Not returning <code>Promises</code> could lead to test failures and false positives.</p>
</li>
</ul>
<h2>Debugging Tests</h2><p>While developing or debugging a test, you can set the environment variable <code>THORN_VERBOSE</code> to enable
verbose output from Thorn.</p>
<p>Users of Thorn seeking to debug their tests should set <code>THORN_VERBOSE=1</code>.
<code>THORN_VERBOSE=2</code> is intended for those developing Thorn itself and is not intended for users.</p>
<p>Before commiting tests, <strong>please ensure you run them with THORN_VERBOSE=1</strong> so you can be sure you are making the
HTTP requests and getting the responses back that you expect.</p></article>
</section>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-3">
<div id="toc" class="col-md-3 hidden-xs hidden-sm hidden-md"></div>
</div>
</div>
</div>
<div class="modal fade" id="searchResults">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Search results</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<footer>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a>
on 2017-09-27T13:51:56-07:00
using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>
<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>
<script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
<script>
$( function () {
$( "[id*='$']" ).each( function () {
var $this = $( this );
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
} );
$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
var $this = $( this );
var example = $this.find( "code" );
exampleText = example.html();
var lang = /{@lang (.*?)}/.exec( exampleText );
if ( lang && lang[1] ) {
exampleText = exampleText.replace( lang[0], "" );
example.html( exampleText );
lang = lang[1];
} else {
var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
lang = langClassMatch ? langClassMatch[1] : "javascript";
}
if ( lang ) {
$this
.addClass( "sunlight-highlight-" + lang )
.addClass( "linenums" )
.html( example.html() );
}
} );
Sunlight.highlightAll( {
lineNumbers : false,
showMenu : true,
enableDoclinks : true
} );
$.catchAnchorLinks( {
navbarOffset: 10
} );
$( "#toc" ).toc( {
anchorName : function ( i, heading, prefix ) {
return $( heading ).attr( "id" ) || ( prefix + i );
},
selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
showAndHide : false,
smoothScrolling: true
} );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
$( '.dropdown-toggle' ).dropdown();
$( "table" ).each( function () {
var $this = $( this );
$this.addClass('table');
} );
} );
</script>
<!--Navigation and Symbol Display-->
<!--Google Analytics-->
<script type="text/javascript">
$(document).ready(function() {
SearcherDisplay.init();
});
</script>
</body>
</html>