forked from ehmad11/netsuite-rest
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrequest.test.ts
More file actions
180 lines (159 loc) · 5.32 KB
/
Copy pathrequest.test.ts
File metadata and controls
180 lines (159 loc) · 5.32 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
import "dotenv/config";
import NetsuiteApiClient from "../src/client.js";
import { describe, expect, beforeAll, it } from "vitest";
describe("Test request method", () => {
let client: NetsuiteApiClient;
let clientWithBaseUrl: NetsuiteApiClient;
beforeAll(async () => {
if (
process.env.consumer_key == undefined ||
process.env.consumer_secret_key == undefined ||
process.env.token == undefined ||
process.env.token_secret == undefined ||
process.env.realm == undefined ||
process.env.base_url == undefined
) {
throw new Error("Please create a `.env` file based on `.env.sample`");
}
client = new NetsuiteApiClient({
consumer_key: process.env.consumer_key,
consumer_secret_key: process.env.consumer_secret_key,
token: process.env.token,
token_secret: process.env.token_secret,
realm: process.env.realm,
});
// with base_url
clientWithBaseUrl = new NetsuiteApiClient({
consumer_key: process.env.consumer_key,
consumer_secret_key: process.env.consumer_secret_key,
token: process.env.token,
token_secret: process.env.token_secret,
realm: process.env.realm,
base_url: process.env.base_url,
});
});
it("should make test request", async () => {
expect.assertions(1);
const response = await client.request({
method: "OPTIONS",
});
expect(response.statusCode).toEqual(204);
});
// it('should make a RESTlet request', async () => {
// expect.assertions(1);
// const response = await client.request({
// method: 'GET',
// restletUrl: process.env.restlet_url,
// heads: {
// 'Content-Type': 'application/json'
// }
// })
// expect(response.statusCode).toEqual(200);
// })
it("should make GET request - GET Customers", async () => {
expect.assertions(4);
const response = await client.request({
path: "record/v1/customer/",
});
expect(response.statusCode).toEqual(200);
expect(response.data).toBeDefined();
expect(response.data.items).toBeDefined();
expect(response.data.count).toBeDefined();
});
it("should make POST request - POST Customers", async () => {
const response = await client.request({
path: "record/v1/customer/",
method: "POST",
body: JSON.stringify({
entityStatus: 6,
firstName: "firstName",
lastName: "lastName",
subsidiary: "1",
companyName: "netsuite-api-client-integration-test",
}),
});
expect(response.statusCode).toEqual(204);
expect(response.data).toBeDefined();
expect(response.data).toBeNull();
expect(response.headers.location).toBeDefined();
const location = response.headers.location as string;
// extract id from location string
const id = location.split("/").pop();
// delete the created company
const responseDelete = await client.request({
path: `record/v1/customer/${id}`,
method: "DELETE",
});
expect(responseDelete.statusCode).toEqual(204);
});
it("should make POST request - SuiteQL Query", async () => {
expect.assertions(4);
const response = await client.request({
path: "query/v1/suiteql?limit=5",
method: "POST",
body: `{
"q": "SELECT id, companyName, email, dateCreated FROM customer WHERE dateCreated >= '01/01/2019' AND dateCreated < '01/01/2020'"
}`,
});
expect(response.statusCode).toEqual(200);
expect(response.data).toBeDefined();
expect(response.data.items).toBeDefined();
expect(response.data.count).toBeDefined();
});
it("should work with base_url", async () => {
expect.assertions(2);
expect(process.env.base_url).toBeDefined();
const response = await clientWithBaseUrl.request({
method: "OPTIONS",
});
expect(response.statusCode).toEqual(204);
});
it("should work with additional headers", async () => {
expect.assertions(1);
const response = await client.request({
method: "OPTIONS",
heads: {
foo: "foo",
},
});
expect(response.statusCode).toEqual(204);
});
it("should reject with meaningful error if bad url", async () => {
expect.assertions(1);
await expect(() =>
client.request({
path: "record/v1/bad-path",
}),
).rejects.toThrowError("Record type 'bad-path' does not exist");
});
it("should reject with meaningful error if bad object", async () => {
expect.assertions(1);
await expect(() =>
client.request({
path: "record/v1/customer/-1",
}),
).rejects.toThrowError(
"The record instance does not exist. Provide a valid record instance ID.",
);
});
it("should reject with meaningful error if bad request (wrong body)", async () => {
expect.assertions(1);
await expect(() =>
client.request({
method: "post",
path: "record/v1/customer",
body: "bad body",
}),
).rejects.toThrowError("Invalid content in the request body");
});
it("should reject with meaningful error if bad request (missing fields)", async () => {
expect.assertions(1);
await expect(() =>
client.request({
method: "post",
path: "record/v1/customer",
body: JSON.stringify({}),
}),
).rejects.toThrowError("Error while accessing a resource. Please enter value(s) for: ");
});
});