forked from cap-js-community/odata-v2-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.test.js
105 lines (99 loc) · 3.49 KB
/
attachment.test.js
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
"use strict";
const cds = require("@sap/cds");
const supertest = require("supertest");
// eslint-disable-next-line no-restricted-modules
const fs = require("fs");
const util = require("./_env/util/request");
cds.test(__dirname + "/_env");
let request;
describe("attachment", () => {
beforeAll(async () => {
await global._init;
request = supertest(cds.app.server);
});
it("POST attachment to attachment folder", async () => {
let response = await util.callWrite(request, "/odata/v2/attachment/AttachmentFolder", {
name: "Folder1",
});
expect(response.statusCode).toEqual(201);
expect(response.body).toBeDefined();
expect(response.body.d).toBeDefined();
const folderId = response.body.d.ID;
const file = fs.readFileSync(__dirname + "/_env/srv/init/assets/file.png");
response = await util.callBinary(
request,
`/odata/v2/attachment/AttachmentFolder(guid'${folderId}')/attachments`,
file,
false,
{
"content-type": "image/png",
},
);
expect(response.statusCode).toEqual(201);
expect(response.body).toBeDefined();
expect(response.body.d.ID).toBeDefined();
const attachmentId = response.body.d.ID;
expect(response.body.d).toEqual({
ID: attachmentId,
folder_ID: folderId,
imageType: "image/png",
folder: {
__deferred: {
uri: `http://${response.request.host.replace(
"127.0.0.1",
"localhost",
)}/odata/v2/attachment/Attachment(guid'${attachmentId}')/folder`,
},
},
__metadata: {
type: "attachment.AttachmentService.Attachment",
uri: `http://${response.request.host.replace(
"127.0.0.1",
"localhost",
)}/odata/v2/attachment/Attachment(guid'${attachmentId}')`,
media_src: `http://${response.request.host.replace(
"127.0.0.1",
"localhost",
)}/odata/v2/attachment/Attachment(guid'${attachmentId}')/$value`,
content_type: "image/png",
},
});
response = await util.callRead(request, `/odata/v2/attachment/AttachmentFolder(guid'${folderId}')/attachments`);
expect(response.statusCode).toEqual(200);
expect(response.body).toBeDefined();
expect(response.body.d).toEqual({
results: [
{
ID: attachmentId,
__metadata: {
content_type: "image/png",
media_src: `http://${response.request.host.replace(
"127.0.0.1",
"localhost",
)}/odata/v2/attachment/Attachment(guid'${attachmentId}')/$value`,
type: "attachment.AttachmentService.Attachment",
uri: `http://${response.request.host.replace(
"127.0.0.1",
"localhost",
)}/odata/v2/attachment/Attachment(guid'${attachmentId}')`,
},
folder: {
__deferred: {
uri: `http://${response.request.host.replace(
"127.0.0.1",
"localhost",
)}/odata/v2/attachment/Attachment(guid'${attachmentId}')/folder`,
},
},
folder_ID: folderId,
imageType: "image/png",
},
],
});
const readResponse = await util.callRead(request, `/odata/v2/attachment/Attachment(guid'${attachmentId}')/file`);
expect(readResponse.statusCode).toEqual(200);
expect(readResponse.headers["content-type"]).toEqual("image/png");
expect(readResponse.body.length).toEqual(17686);
expect(readResponse.body).toEqual(file);
});
});