Skip to content

Commit 62e7068

Browse files
irinaAbramova-devirinaP-local
authored andcommitted
#RI-4815 add tests
1 parent cef594c commit 62e7068

File tree

3 files changed

+144
-11
lines changed

3 files changed

+144
-11
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {
2+
describe,
3+
before,
4+
Joi,
5+
deps,
6+
requirements,
7+
generateInvalidDataTestCases,
8+
validateInvalidDataTestCase,
9+
getMainCheckFn
10+
} from '../deps';
11+
const { server, request, constants, rte } = deps;
12+
13+
// endpoint to test
14+
const endpoint = (instanceId = constants.TEST_INSTANCE_ID) =>
15+
request(server).post(`/${constants.API.DATABASES}/${instanceId}/string/download-value`);
16+
17+
// input data schema
18+
const dataSchema = Joi.object({
19+
keyName: Joi.string().allow('').required(),
20+
}).strict();
21+
22+
const validInputData = {
23+
keyName: constants.TEST_STRING_KEY_1,
24+
};
25+
26+
const mainCheckFn = getMainCheckFn(endpoint);
27+
28+
describe('POST /databases/:instanceId/string/download-value', () => {
29+
describe('Main', () => {
30+
before(() => rte.data.generateKeys(true));
31+
32+
describe('Validation', () => {
33+
generateInvalidDataTestCases(dataSchema, validInputData).map(
34+
validateInvalidDataTestCase(endpoint, dataSchema),
35+
);
36+
});
37+
38+
describe('Common', () => {
39+
[
40+
{
41+
name: 'Should download value',
42+
data: {
43+
keyName: constants.TEST_STRING_KEY_1,
44+
},
45+
responseHeaders: {
46+
'content-type': 'application/octet-stream',
47+
'content-disposition': 'attachment;filename="string_value.txt"',
48+
'access-control-expose-headers': 'Content-Disposition',
49+
},
50+
responseBody: Buffer.from(`"${constants.TEST_STRING_VALUE_1}"`),
51+
},
52+
{
53+
name: 'Should return an error when incorrect type',
54+
data: {
55+
keyName: constants.TEST_LIST_KEY_1,
56+
},
57+
statusCode: 400,
58+
responseBody: {
59+
statusCode: 400,
60+
error: 'Bad Request',
61+
},
62+
},
63+
{
64+
name: 'Should return NotFound error if instance id does not exists',
65+
endpoint: () => endpoint(constants.TEST_NOT_EXISTED_INSTANCE_ID),
66+
data: {
67+
keyName: constants.TEST_STRING_KEY_1,
68+
},
69+
statusCode: 404,
70+
responseBody: {
71+
statusCode: 404,
72+
error: 'Not Found',
73+
message: 'Invalid database instance id.',
74+
},
75+
},
76+
].map(mainCheckFn);
77+
});
78+
79+
describe('ACL', () => {
80+
requirements('rte.acl');
81+
before(async () => rte.data.setAclUserRules('~* +@all'));
82+
83+
[
84+
{
85+
name: 'Should download value',
86+
endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID),
87+
data: {
88+
keyName: constants.TEST_STRING_KEY_1,
89+
},
90+
responseHeaders: {
91+
'content-type': 'application/octet-stream',
92+
'content-disposition': 'attachment;filename="string_value.txt"',
93+
'access-control-expose-headers': 'Content-Disposition',
94+
},
95+
responseBody: Buffer.from(`"${constants.TEST_STRING_VALUE_1}"`),
96+
},
97+
{
98+
name: 'Should throw error if no permissions for "set" command',
99+
endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID),
100+
data: {
101+
keyName: constants.getRandomString(),
102+
value: constants.getRandomString(),
103+
},
104+
statusCode: 403,
105+
responseBody: {
106+
statusCode: 403,
107+
error: 'Forbidden',
108+
},
109+
before: () => rte.data.setAclUserRules('~* +@all -get')
110+
},
111+
].map(mainCheckFn);
112+
});
113+
});
114+
});

redisinsight/api/test/api/string/POST-databases-id-string-get_value.test.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ describe('POST /databases/:instanceId/string/get-value', () => {
4848
value: constants.TEST_STRING_VALUE_BIN_UTF8_1,
4949
},
5050
},
51-
{
52-
name: 'Should return part of value in utf8 (specified length)',
53-
data: {
54-
keyName: constants.TEST_STRING_KEY_BIN_BUF_OBJ_1,
55-
stringMaxLen: constants.TEST_STRING_KEY_LENGTH
56-
},
57-
responseBody: {
58-
keyName: constants.TEST_STRING_KEY_BIN_UTF8_1,
59-
value: constants.TEST_STRING_VALUE_BIN_UTF8_1.slice(0, constants.TEST_STRING_KEY_LENGTH),
60-
},
61-
},
6251
{
6352
name: 'Should return value in utf8',
6453
query: {
@@ -132,6 +121,17 @@ describe('POST /databases/:instanceId/string/get-value', () => {
132121
value: constants.TEST_STRING_VALUE_1,
133122
},
134123
},
124+
{
125+
name: 'Should get part of value (specified length)',
126+
data: {
127+
keyName: constants.TEST_STRING_KEY_1,
128+
stringMaxLen: constants.TEST_STRING_KEY_LENGTH
129+
},
130+
responseBody: {
131+
keyName: constants.TEST_STRING_KEY_1,
132+
value: constants.TEST_STRING_VALUE_1.slice(0, constants.TEST_STRING_KEY_LENGTH),
133+
},
134+
},
135135
{
136136
name: 'Should return an error when incorrect type',
137137
data: {
@@ -144,6 +144,18 @@ describe('POST /databases/:instanceId/string/get-value', () => {
144144
// message: 'WRONGTYPE Operation against a key holding the wrong kind of value',
145145
},
146146
},
147+
{
148+
name: 'Should return an error when incorrect string length',
149+
data: {
150+
keyName: constants.TEST_STRING_KEY_1,
151+
stringMaxLen: 0
152+
},
153+
statusCode: 400,
154+
responseBody: {
155+
statusCode: 400,
156+
error: 'Bad Request',
157+
},
158+
},
147159
{
148160
name: 'Should return NotFound error if instance id does not exists',
149161
endpoint: () => endpoint(constants.TEST_NOT_EXISTED_INSTANCE_ID),

redisinsight/api/test/helpers/test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface ITestCaseInput {
3232
statusCode?: number;
3333
responseSchema?: Joi.AnySchema;
3434
responseBody?: any;
35+
responseHeaders?: object,
3536
checkFn?: Function;
3637
preconditionFn?: Function;
3738
postCheckFn?: Function;
@@ -51,6 +52,7 @@ export const validateApiCall = async function ({
5152
statusCode = 200,
5253
responseSchema,
5354
responseBody,
55+
responseHeaders,
5456
checkFn,
5557
}: ITestCaseInput): Promise<any> {
5658
const request = endpoint();
@@ -93,6 +95,11 @@ export const validateApiCall = async function ({
9395

9496
expect(response.res.statusCode).to.eq(statusCode)
9597

98+
// validate response headers if passed
99+
if (responseHeaders) {
100+
expect(response.res.headers).to.include(responseHeaders);
101+
}
102+
96103
// validate response schema if passed
97104
if (responseSchema) {
98105
Joi.assert(response.body, responseSchema);

0 commit comments

Comments
 (0)