UNPKG

7.42 kBJavaScriptView Raw
1/* eslint-env mocha */
2
3'use strict'
4
5const expect = require('chai').expect
6const tymly = require('@wmfs/tymly')
7const path = require('path')
8const process = require('process')
9const sqlScriptRunner = require('./fixtures/sql-script-runner.js')
10const STATE_MACHINE_NAME = 'tymlyTest_search_1_0'
11
12process.on('unhandledRejection', (reason, p) => {
13 console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
14 // application specific logging, throwing an error, or other logic here
15})
16
17describe('tymly-solr-plugin search state resource tests', function () {
18 this.timeout(process.env.TIMEOUT || 5000)
19
20 let tymlyService; let statebox = null; let client; let rbacAdmin
21
22 before((done) => {
23 if (process.env.PG_CONNECTION_STRING && !/^postgres:\/\/[^:]+:[^@]+@(?:localhost|127\.0\.0\.1).*$/.test(process.env.PG_CONNECTION_STRING)) {
24 console.log(`Skipping tests due to unsafe PG_CONNECTION_STRING value (${process.env.PG_CONNECTION_STRING})`)
25 this.skip()
26 done()
27 }
28
29 tymly.boot(
30 {
31 pluginPaths: [
32 path.resolve(__dirname, './../lib'),
33 require.resolve('@wmfs/tymly-pg-plugin'),
34 require.resolve('@wmfs/tymly-rbac-plugin'),
35 require.resolve('@wmfs/tymly-cardscript-plugin')
36 ],
37 blueprintPaths: [
38 path.resolve(__dirname, './fixtures/school-blueprint')
39 ],
40 config: {
41 solrSchemaFields: [
42 'id',
43 'actorName',
44 'characterName',
45 'roles',
46 'launches'
47 ]
48 }
49 },
50 function (err, tymlyServices) {
51 expect(err).to.eql(null)
52 tymlyService = tymlyServices.tymly
53 statebox = tymlyServices.statebox
54 rbacAdmin = tymlyServices.rbacAdmin
55 client = tymlyServices.storage.client
56 done()
57 }
58 )
59 })
60
61 describe('setup', () => {
62 it('create test resources', function (done) {
63 sqlScriptRunner(
64 './db-scripts/setup.sql',
65 client,
66 function (err) {
67 expect(err).to.equal(null)
68 if (err) {
69 done(err)
70 } else {
71 done()
72 }
73 }
74 )
75 })
76
77 it('John Smith is the boss and a minor', () => {
78 return rbacAdmin.ensureUserRoles(
79 'john.smith',
80 ['tymlyTest_boss', 'tymlyTest_minor']
81 )
82 })
83
84 it('ensure Jane Smith is a minor', () => {
85 return rbacAdmin.ensureUserRoles(
86 'jane.smith',
87 ['tymlyTest_minor']
88 )
89 })
90 }) // setup
91
92 describe('search', () => {
93 describe('user with boss and minor role', () => {
94 it('no input returns everything', async () => {
95 const searchResults = await search(null, 'john.smith')
96 expect(searchResults.totalHits).to.eql(24)
97 })
98 it('search for staff', async () => {
99 const searchResults = await search('Hagrid', 'john.smith')
100 expect(searchResults.totalHits).to.eql(1)
101 expect(searchResults.results[0].character_name).to.eql('RUBEUS HAGRID')
102 expect(searchResults.results[0].launches.length).to.equal(1)
103 })
104 it('search for student', async () => {
105 const searchResults = await search('Hermione', 'john.smith')
106
107 expect(searchResults.totalHits).to.eql(1)
108 expect(searchResults.results[0].character_name).to.eql('HERMIONE GRANGER')
109 expect(searchResults.results[0].launches.length).to.equal(2)
110 })
111 it('search for muggle', async () => {
112 const searchResults = await search('William', 'john.smith')
113
114 expect(searchResults.totalHits).to.eql(1)
115 expect(searchResults.results[0].character_name).to.eql('Themself')
116 expect(searchResults.results[0].launches.length).to.equal(2)
117 })
118 })
119
120 describe('user with minor role', () => {
121 it('no input returns all but staff', async () => {
122 const searchResults = await search(null, 'jane.smith')
123
124 expect(searchResults.totalHits).to.eql(15)
125 expect(searchResults.results.length).to.eql(15)
126 })
127 it('search for staff', async () => {
128 const searchResults = await search('Hagrid', 'jane.smith')
129 expect(searchResults.totalHits).to.eql(0)
130 })
131 it('search for student', async () => {
132 const searchResults = await search('Hermione', 'jane.smith')
133
134 expect(searchResults.totalHits).to.eql(1)
135 expect(searchResults.results[0].character_name).to.eql('HERMIONE GRANGER')
136 expect(searchResults.results[0].launches.length).to.equal(1)
137 })
138 it('search for muggle', async () => {
139 const searchResults = await search('William', 'jane.smith')
140
141 expect(searchResults.totalHits).to.eql(1)
142 expect(searchResults.results[0].character_name).to.eql('Themself')
143 expect(searchResults.results[0].launches.length).to.equal(1)
144 })
145 })
146
147 describe('user with no role', () => {
148 it('no input returns only muggles', async () => {
149 const searchResults = await search(null, 'jim.smith')
150
151 expect(searchResults.totalHits).to.eql(5)
152 expect(searchResults.results.length).to.eql(5)
153 })
154 it('search for staff', async () => {
155 const searchResults = await search('Hagrid', 'jim.smith')
156 expect(searchResults.totalHits).to.eql(0)
157 })
158 it('search for student', async () => {
159 const searchResults = await search('Hermione', 'jim.smith')
160
161 expect(searchResults.totalHits).to.eql(0)
162 })
163 it('search for muggle', async () => {
164 const searchResults = await search('William', 'jim.smith')
165
166 expect(searchResults.totalHits).to.eql(1)
167 expect(searchResults.results[0].character_name).to.eql('Themself')
168 expect(searchResults.results[0].launches.length).to.equal(1)
169 })
170 })
171 describe('no user id', () => {
172 it('fail to search when no user id', async () => {
173 const executionDescription = await statebox.startExecution(
174 {}, // input
175 STATE_MACHINE_NAME, // state machine name
176 {
177 sendResponse: 'COMPLETE'
178 }
179 )
180
181 expect(executionDescription.status).to.eql('FAILED')
182 expect(executionDescription.errorCode).to.eql('noUserIdSearchFail')
183 expect(executionDescription.errorMessage).to.eql('No user ID found when trying to search.')
184 })
185 })
186 })
187
188 describe('teardown', () => {
189 it('cleanup test resources', function (done) {
190 sqlScriptRunner(
191 './db-scripts/cleanup.sql',
192 client,
193 function (err) {
194 expect(err).to.equal(null)
195 if (err) {
196 done(err)
197 } else {
198 done()
199 }
200 }
201 )
202 })
203 })
204
205 after(async () => {
206 await tymlyService.shutdown()
207 })
208
209 async function search (query, userId) {
210 const executionDescription = await statebox.startExecution(
211 {
212 query: query,
213 limit: 100
214 }, // input
215 STATE_MACHINE_NAME, // state machine name
216 {
217 sendResponse: 'COMPLETE',
218 userId: userId
219 } // options
220 )
221
222 expect(executionDescription.currentStateName).to.eql('Search')
223 expect(executionDescription.currentResource).to.eql('module:search')
224 expect(executionDescription.stateMachineName).to.eql(STATE_MACHINE_NAME)
225 expect(executionDescription.status).to.eql('SUCCEEDED')
226
227 return executionDescription.ctx.searchResults
228 } // search
229})