|
| 1 | +const crypto = require('crypto'); |
| 2 | +const { Joi } = require('express-validation'); |
| 3 | +const jestJoi = require('jest-joi'); |
| 4 | +expect.extend(jestJoi.matchers); |
| 5 | + |
| 6 | +const schema = require('../../../src/validators/object').schema; |
| 7 | +const { scheme, type } = require('../../../src/validators/common'); |
| 8 | + |
| 9 | +describe('deleteObject', () => { |
| 10 | + |
| 11 | + describe('params', () => { |
| 12 | + const params = schema.deleteObject.params.describe(); |
| 13 | + |
| 14 | + describe('objId', () => { |
| 15 | + const objId = params.keys.objId; |
| 16 | + |
| 17 | + it('is the expected schema', () => { |
| 18 | + expect(objId).toEqual(type.uuidv4.describe()); |
| 19 | + }); |
| 20 | + }); |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('headObject', () => { |
| 25 | + |
| 26 | + describe('params', () => { |
| 27 | + const params = schema.headObject.params.describe(); |
| 28 | + |
| 29 | + describe('objId', () => { |
| 30 | + const objId = params.keys.objId; |
| 31 | + |
| 32 | + it('is the expected schema', () => { |
| 33 | + expect(objId).toEqual(type.uuidv4.describe()); |
| 34 | + }); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('query', () => { |
| 39 | + const query = schema.headObject.query.describe(); |
| 40 | + |
| 41 | + describe('versionId', () => { |
| 42 | + const versionId = query.keys.versionId; |
| 43 | + |
| 44 | + it('is the expected schema', () => { |
| 45 | + expect(versionId).toEqual(type.alphanum.describe()); |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('listObjectVersion', () => { |
| 52 | + |
| 53 | + describe('params', () => { |
| 54 | + const params = schema.listObjectVersion.params.describe(); |
| 55 | + |
| 56 | + describe('objId', () => { |
| 57 | + const objId = params.keys.objId; |
| 58 | + |
| 59 | + it('is the expected schema', () => { |
| 60 | + expect(objId).toEqual(type.uuidv4.describe()); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('readObject', () => { |
| 67 | + |
| 68 | + describe('params', () => { |
| 69 | + const params = schema.readObject.params.describe(); |
| 70 | + |
| 71 | + describe('objId', () => { |
| 72 | + const objId = params.keys.objId; |
| 73 | + |
| 74 | + it('is the expected schema', () => { |
| 75 | + expect(objId).toEqual(type.uuidv4.describe()); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('query', () => { |
| 81 | + const query = schema.readObject.query.describe(); |
| 82 | + |
| 83 | + describe('versionId', () => { |
| 84 | + const versionId = query.keys.versionId; |
| 85 | + |
| 86 | + it('is the expected schema', () => { |
| 87 | + expect(versionId).toEqual(type.alphanum.describe()); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('expiresIn', () => { |
| 92 | + const expiresIn = query.keys.expiresIn; |
| 93 | + |
| 94 | + it('is the expected schema', () => { |
| 95 | + expect(expiresIn).toEqual(Joi.number().describe()); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('download', () => { |
| 100 | + const download = query.keys.download; |
| 101 | + |
| 102 | + it('is the expected schema', () => { |
| 103 | + expect(download).toEqual(type.truthy.describe()); |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('searchObjects', () => { |
| 110 | + |
| 111 | + describe('query', () => { |
| 112 | + const query = schema.searchObjects.query.describe(); |
| 113 | + |
| 114 | + describe('objId', () => { |
| 115 | + const objId = query.keys.objId; |
| 116 | + |
| 117 | + it('is the expected schema', () => { |
| 118 | + expect(objId).toEqual(scheme.guid.describe()); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('originalName', () => { |
| 123 | + const originalName = query.keys.originalName; |
| 124 | + |
| 125 | + it('is the expected schema', () => { |
| 126 | + expect(originalName).toEqual(type.alphanum.describe()); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('path', () => { |
| 131 | + const path = query.keys.path; |
| 132 | + |
| 133 | + it('is a string', () => { |
| 134 | + expect(path).toBeTruthy(); |
| 135 | + expect(path.type).toEqual('string'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('has a max length of 1024', () => { |
| 139 | + expect(Array.isArray(path.rules)).toBeTruthy(); |
| 140 | + expect(path.rules).toHaveLength(1); |
| 141 | + expect(path.rules).toEqual(expect.arrayContaining([ |
| 142 | + expect.objectContaining({ |
| 143 | + 'args': { |
| 144 | + 'limit': 1024 |
| 145 | + }, |
| 146 | + 'name': 'max' |
| 147 | + }), |
| 148 | + ])); |
| 149 | + }); |
| 150 | + |
| 151 | + it('matches the schema', () => { |
| 152 | + expect('some/path/to/object').toMatchSchema(Joi.string().max(1024)); |
| 153 | + }); |
| 154 | + |
| 155 | + it('must be less than or equal to 1024 characters long', () => { |
| 156 | + const reallyLongStr = crypto.randomBytes(1025).toString('hex'); |
| 157 | + expect(reallyLongStr).not.toMatchSchema(Joi.string().max(1024)); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('mimeType', () => { |
| 162 | + const mimeType = query.keys.mimeType; |
| 163 | + |
| 164 | + it('is a string', () => { |
| 165 | + expect(mimeType).toBeTruthy(); |
| 166 | + expect(mimeType.type).toEqual('string'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('has a max length of 255', () => { |
| 170 | + expect(Array.isArray(mimeType.rules)).toBeTruthy(); |
| 171 | + expect(mimeType.rules).toHaveLength(1); |
| 172 | + expect(mimeType.rules).toEqual(expect.arrayContaining([ |
| 173 | + expect.objectContaining({ |
| 174 | + 'args': { |
| 175 | + 'limit': 255 |
| 176 | + }, |
| 177 | + 'name': 'max' |
| 178 | + }), |
| 179 | + ])); |
| 180 | + }); |
| 181 | + |
| 182 | + it('matches the schema', () => { |
| 183 | + expect('image/jpeg').toMatchSchema(Joi.string().max(255)); |
| 184 | + }); |
| 185 | + |
| 186 | + it('must be less than or equal to 255 characters long', () => { |
| 187 | + const longStr = crypto.randomBytes(256).toString('hex'); |
| 188 | + expect(longStr).not.toMatchSchema(Joi.string().max(255)); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + describe('public', () => { |
| 193 | + const publicKey = query.keys.public; |
| 194 | + |
| 195 | + it('is the expected schema', () => { |
| 196 | + expect(publicKey).toEqual(type.truthy.describe()); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('active', () => { |
| 201 | + const active = query.keys.active; |
| 202 | + |
| 203 | + it('is the expected schema', () => { |
| 204 | + expect(active).toEqual(type.truthy.describe()); |
| 205 | + }); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
| 210 | +describe('togglePublic', () => { |
| 211 | + |
| 212 | + describe('params', () => { |
| 213 | + const params = schema.togglePublic.params.describe(); |
| 214 | + |
| 215 | + describe('objId', () => { |
| 216 | + const objId = params.keys.objId; |
| 217 | + |
| 218 | + it('is the expected schema', () => { |
| 219 | + expect(objId).toEqual(type.uuidv4.describe()); |
| 220 | + }); |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + describe('query', () => { |
| 225 | + const query = schema.togglePublic.query.describe(); |
| 226 | + |
| 227 | + describe('public', () => { |
| 228 | + const publicKey = query.keys.public; |
| 229 | + |
| 230 | + it('is the expected schema', () => { |
| 231 | + expect(publicKey).toEqual(type.truthy.describe()); |
| 232 | + }); |
| 233 | + }); |
| 234 | + }); |
| 235 | +}); |
0 commit comments