-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathterms-test.js
More file actions
442 lines (398 loc) · 15 KB
/
terms-test.js
File metadata and controls
442 lines (398 loc) · 15 KB
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
import { describe, it, beforeEach } from 'mocha'
import { expect } from 'chai'
import { jsonReader } from '../utility/fileOperations/readwrite'
import { contentstackClient } from '../utility/ContentstackClient.js'
import { environmentCreate } from '../mock/environment.js'
import { stageBranch } from '../mock/branch.js'
var client = {}
const taxonomy = {
uid: 'taxonomy_testing',
name: 'taxonomy testing',
description: 'Description for Taxonomy testing'
}
const termString = 'term'
const term = {
term: {
uid: 'term_test',
name: 'Term test',
parent_uid: null
}
}
const childTerm1 = {
term: {
uid: 'term_test_child1',
name: 'Term test1',
parent_uid: 'term_test'
}
}
const childTerm2 = {
term: {
uid: 'term_test_child2',
name: 'Term test2',
parent_uid: 'term_test_child1'
}
}
var termUid = term.term.uid
describe('Terms API Test', () => {
beforeEach(() => {
const user = jsonReader('loggedinuser.json')
client = contentstackClient(user.authtoken)
})
it('should create taxonomy', async () => {
const response = await client.stack({ api_key: process.env.API_KEY }).taxonomy().create({ taxonomy })
await client.stack({ api_key: process.env.API_KEY }).environment().create(environmentCreate)
expect(response.uid).to.be.equal(taxonomy.uid)
await new Promise(resolve => setTimeout(resolve, 5000))
}, 10000)
it('should create term', async () => {
const response = await makeTerms(taxonomy.uid).create(term)
expect(response.uid).to.be.equal(term.term.uid)
await new Promise(resolve => setTimeout(resolve, 15000))
})
it('should create child term 1', async () => {
const response = await makeTerms(taxonomy.uid).create(childTerm1)
expect(response.uid).to.be.equal(childTerm1.term.uid)
await new Promise(resolve => setTimeout(resolve, 15000))
})
it('should create child term 2', async () => {
const response = await makeTerms(taxonomy.uid).create(childTerm2)
expect(response.uid).to.be.equal(childTerm2.term.uid)
await new Promise(resolve => setTimeout(resolve, 15000))
})
it('should query and get all terms', done => {
makeTerms(taxonomy.uid).query().find()
.then((response) => {
expect(response.items).to.be.an('array')
expect(response.items[0].uid).not.to.be.equal(null)
expect(response.items[0].name).not.to.be.equal(null)
done()
})
.catch(done)
})
it('should fetch term of the term uid passed', done => {
makeTerms(taxonomy.uid, term.term.uid).fetch()
.then((response) => {
expect(response.uid).to.be.equal(termUid)
expect(response.name).not.to.be.equal(null)
expect(response.created_by).not.to.be.equal(null)
expect(response.updated_by).not.to.be.equal(null)
done()
})
.catch(done)
})
it('should update term of the term uid passed', done => {
makeTerms(taxonomy.uid, termUid).fetch()
.then((term) => {
term.name = 'update name'
return term.update()
})
.then((response) => {
expect(response.uid).to.be.equal(termUid)
expect(response.name).to.be.equal('update name')
expect(response.created_by).not.to.be.equal(null)
expect(response.updated_by).not.to.be.equal(null)
done()
})
.catch(done)
})
it('should get the ancestors of the term uid passed', done => {
makeTerms(taxonomy.uid, childTerm1.term.uid).ancestors()
.then((response) => {
expect(response.terms[0].uid).not.to.be.equal(null)
expect(response.terms[0].name).not.to.be.equal(null)
expect(response.terms[0].created_by).not.to.be.equal(null)
expect(response.terms[0].updated_by).not.to.be.equal(null)
done()
})
.catch(done)
})
it('should get the descendants of the term uid passed', done => {
makeTerms(taxonomy.uid, childTerm1.term.uid).descendants()
.then((response) => {
expect(response.terms.uid).not.to.be.equal(null)
expect(response.terms.name).not.to.be.equal(null)
expect(response.terms.created_by).not.to.be.equal(null)
expect(response.terms.updated_by).not.to.be.equal(null)
done()
})
.catch(done)
})
it('should publish with api_version', done => {
const publishData = {
locales: ['en-us'],
environments: ['development'],
items: [
{
uid: taxonomy.uid,
term_uid: 'term_test'
},
{
uid: taxonomy.uid,
term_uid: 'term_test_child1'
},
{
uid: taxonomy.uid,
term_uid: 'term_test_child2'
}
]
}
makeTaxonomy()
.publish(publishData, '3.2')
.then((response) => {
expect(response.notice).to.not.equal(null)
expect(response.job_id).to.not.equal(undefined)
done()
})
.catch(done)
})
it('should search the term with the string passed', done => {
makeTerms(taxonomy.uid).search(termString)
.then((response) => {
expect(response.terms).to.be.an('array')
done()
})
.catch(done)
})
it('should move the term to parent uid passed', done => {
const term = {
parent_uid: 'term_test_child1',
order: 1
}
makeTerms(taxonomy.uid, childTerm2.term.uid).move({ term, force: true })
.then(async (term) => {
expect(term.parent_uid).to.not.equal(null)
done()
})
.catch(done)
})
it('should get term locales', done => {
makeTerms(taxonomy.uid, term.term.uid).locales()
.then((response) => {
expect(response).to.have.property('terms')
expect(response.terms).to.be.an('array')
done()
})
.catch(done)
})
it('should localize term', done => {
const localizedTerm = {
term: {
uid: term.term.uid,
name: 'Term test localized',
parent_uid: null
}
}
makeTerms(taxonomy.uid, term.term.uid).localize(localizedTerm, { locale: 'hi-in' })
.then((response) => {
expect(response.uid).to.be.equal(term.term.uid)
expect(response.locale).to.be.equal('hi-in')
done()
})
.catch(done)
})
it('should delete of the term uid passed', done => {
makeTerms(taxonomy.uid, term.term.uid).delete({ force: true })
.then((response) => {
expect(response.status).to.be.equal(204)
done()
})
.catch(done)
})
it('should delete taxonomy', async () => {
const taxonomyResponse = await client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomy.uid).delete({ force: true })
expect(taxonomyResponse.status).to.be.equal(204)
})
})
function makeTerms (taxonomyUid, termUid = null) {
return client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomyUid).terms(termUid)
}
function makeTaxonomy () {
return client.stack({ api_key: process.env.API_KEY }).taxonomy()
}
describe('Terms Query Parameters Sanity Tests', () => {
beforeEach(async () => {
const user = jsonReader('loggedinuser.json')
client = contentstackClient(user.authtoken)
// Ensure taxonomy exists before running query tests
try {
await client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomy.uid).fetch()
} catch (error) {
// If taxonomy doesn't exist, try to use an existing one first
if (error.status === 404) {
try {
// Try to use an existing taxonomy if available
const existingTaxonomies = await client.stack({ api_key: process.env.API_KEY }).taxonomy().query().find()
if (existingTaxonomies.items.length > 0) {
// Use the first existing taxonomy
taxonomy.uid = existingTaxonomies.items[0].uid
console.log(`Using existing taxonomy: ${taxonomy.uid}`)
} else {
// Create a new taxonomy if none exist
await client.stack({ api_key: process.env.API_KEY }).taxonomy().create({ taxonomy })
await new Promise(resolve => setTimeout(resolve, 5000))
}
} catch (createError) {
// If creation fails, try to create the original taxonomy
await client.stack({ api_key: process.env.API_KEY }).taxonomy().create({ taxonomy })
await new Promise(resolve => setTimeout(resolve, 5000))
}
}
}
// Create some test terms if they don't exist
try {
const existingTerms = await makeTerms(taxonomy.uid).query().find()
if (existingTerms.items.length === 0) {
// Create a test term
await makeTerms(taxonomy.uid).create(term)
await new Promise(resolve => setTimeout(resolve, 2000))
}
} catch (error) {
// If terms query fails, try to create a term anyway
try {
await makeTerms(taxonomy.uid).create(term)
await new Promise(resolve => setTimeout(resolve, 2000))
} catch (createError) {
// Ignore creation errors - terms might already exist
// This is expected behavior for test setup
if (createError.status !== 422) {
console.log('Term creation failed, continuing with tests:', createError.message)
}
}
// Log the original error for debugging but don't fail the test
console.log('Terms query failed during setup, continuing with tests:', error.message)
}
})
it('should get terms with locale parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ locale: 'en-us' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with branch parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ branch: 'main' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with include_fallback parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ include_fallback: true })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with fallback_locale parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ fallback_locale: 'en-us' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with depth parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ depth: 2 })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with include_children_count parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ include_children_count: true })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with include_referenced_entries_count parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ include_referenced_entries_count: true })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with include_count parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ include_count: true })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
// Count property might not be available in all environments
if (terms.count !== undefined) {
expect(terms).to.have.property('count')
}
})
it('should get terms with include_order parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ include_order: true })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with asc parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ asc: 'name' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with desc parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ desc: 'name' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with query parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ query: 'term' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with typeahead parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ typeahead: 'term' })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with deleted parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ deleted: true })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with skip and limit parameters', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ skip: 0, limit: 10 })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with taxonomy_uuid parameter', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({ taxonomy_uuid: taxonomy.uid })
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
})
it('should get terms with multiple parameters', async () => {
const terms = await makeTerms(taxonomy.uid).query().find({
locale: 'en-us',
include_children_count: true,
include_count: true,
skip: 0,
limit: 10
})
await client.stack({ api_key: process.env.API_KEY }).environment(environmentCreate.environment.name).delete()
expect(terms).to.have.property('items')
expect(terms.items).to.be.an('array')
// Count property might not be available in all environments
if (terms.count !== undefined) {
expect(terms).to.have.property('count')
}
})
// Cleanup: Delete the taxonomy after query tests
it('should delete taxonomy after query tests', async () => {
try {
const taxonomyResponse = await client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomy.uid).delete({ force: true })
expect(taxonomyResponse.status).to.be.equal(204)
} catch (error) {
// Taxonomy might already be deleted, which is acceptable
if (error.status === 404) {
// Test passes if taxonomy doesn't exist
} else {
throw error
}
}
})
})
describe('Branch creation api Test', () => {
beforeEach(() => {
const user = jsonReader('loggedinuser.json')
client = contentstackClient(user.authtoken)
})
it('should create staging branch', async () => {
const response = await makeBranch().create({ branch: stageBranch })
expect(response.uid).to.be.equal(stageBranch.uid)
expect(response.urlPath).to.be.equal(`/stacks/branches/${stageBranch.uid}`)
expect(response.source).to.be.equal(stageBranch.source)
expect(response.alias).to.not.equal(undefined)
expect(response.fetch).to.not.equal(undefined)
expect(response.delete).to.not.equal(undefined)
await new Promise(resolve => setTimeout(resolve, 15000))
})
})
function makeBranch (uid = null) {
return client.stack({ api_key: process.env.API_KEY }).branch(uid)
}