1 |
|
2 |
|
3 |
|
4 | import path from 'path';
|
5 |
|
6 | import test from 'ava';
|
7 | import dotenv from 'dotenv';
|
8 |
|
9 | import { SquidexClientManager } from '../src/index';
|
10 |
|
11 | dotenv.config();
|
12 |
|
13 | const clientSecret = process.env.SQUIDEX_CLIENT_SECRET;
|
14 | const clientId = process.env.SQUIDEX_CLIENT_ID;
|
15 | const url = process.env.SQUIDEX_CONNECT_URL;
|
16 | const appName = process.env.APP_NAME;
|
17 |
|
18 | const client = new SquidexClientManager(url, appName, clientId, clientSecret);
|
19 |
|
20 | function uniqueString(prefix) {
|
21 | const dateStamp = new Date().toString();
|
22 | return prefix ? `${prefix}-${dateStamp}` : dateStamp;
|
23 | }
|
24 |
|
25 | async function simpleWriteCheck(t, modelName, payload) {
|
26 | const record = await client.CreateAsync(modelName, payload);
|
27 | t.truthy(record.id);
|
28 |
|
29 | const records = await client.RecordsAsync(modelName, { top: 0 });
|
30 | t.true(records.items.length > 0);
|
31 |
|
32 | await client.DeleteAsync(modelName, { id: record.id });
|
33 | }
|
34 |
|
35 | test.before('Get Models', async (t) => {
|
36 | await client.ensureValidClient();
|
37 | const models = client.Models();
|
38 | t.true(Object.keys(models).length > 0);
|
39 | });
|
40 |
|
41 | test.serial('Articles', async (t) => {
|
42 | const text = uniqueString('Testo');
|
43 | const expected = { data: { title: { iv: text } }, publish: true };
|
44 | const article = await client.CreateAsync('Articles', expected);
|
45 | t.truthy(article);
|
46 |
|
47 |
|
48 | const filter = await client.FilterRecordsAsync('Articles', expected, 'title');
|
49 | const created = filter[0];
|
50 | t.true(created !== undefined);
|
51 | t.true(created.data.title.iv === expected.data.title.iv);
|
52 |
|
53 |
|
54 | const update = await client.UpdateAsync('Articles', {
|
55 | id: article.id,
|
56 | data: {
|
57 | title: { iv: created.data.title.iv },
|
58 | text: { iv: 'x' },
|
59 | },
|
60 | });
|
61 | t.true(update.data.text.iv === 'x');
|
62 | t.truthy(article.id);
|
63 |
|
64 |
|
65 | const upload = await client.CreateAssetAsync(path.resolve(__dirname, '../GitHub/power-by.png'));
|
66 |
|
67 |
|
68 | const createOrUpdate = await client.CreateOrUpdateAsync('Articles', {
|
69 | id: article.id,
|
70 | data: {
|
71 | title: { iv: update.data.title.iv },
|
72 | text: { iv: 'y' },
|
73 | image: { iv: [upload.body.id] },
|
74 | },
|
75 | }, 'title');
|
76 | t.true(createOrUpdate.data.text.iv === 'y');
|
77 |
|
78 |
|
79 | const deleteOp = await client.DeleteAsync('Articles', { id: article.id });
|
80 | t.true(deleteOp.status === 204);
|
81 | });
|
82 |
|
83 | test.serial('Tag', async (t) => {
|
84 | await simpleWriteCheck(t, 'Tag', {
|
85 | publish: true,
|
86 | data: {
|
87 | name: { iv: uniqueString('cool') },
|
88 | },
|
89 | });
|
90 | });
|