-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.test.js
More file actions
165 lines (151 loc) · 4.53 KB
/
adapter.test.js
File metadata and controls
165 lines (151 loc) · 4.53 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
import { assertEquals, assertObjectMatch } from './dev_deps.js'
import { ReadableWebToNodeStream } from './deps.js'
import adapter from './adapter.js'
Deno.test('adapter', async (t) => {
const happyMinio = {
makeBucket: (name) => {
if (name === 'test-exists') return Promise.reject({ code: 'BucketAlreadyExists' })
return Promise.resolve()
},
removeBucket: () => Promise.resolve(),
bucketExists: () => Promise.resolve(true),
listBuckets: () =>
Promise.resolve([
{ name: 'test-foo' },
{ name: 'test-bar' },
]),
getObject: () => {
return Promise.resolve(
new ReadableWebToNodeStream(new Response(JSON.stringify({ fizz: 'buzz' })).body),
)
},
putObject: () => Promise.resolve(),
removeObject: () => {
return Promise.resolve()
},
removeObjects: (bucket, keys) => {
if (bucket === 'test-removeBucketTest') {
assertEquals(keys, ['foo', 'bar'])
}
return Promise.resolve()
},
listObjects: () => {
// Mock stream
return {
on: (event, fn) => {
switch (event) {
case 'data': {
fn({ name: 'foo' })
fn({ name: 'bar' })
break
}
case 'end': {
return fn('foo')
}
}
},
}
},
presignedUrl: () => {
return Promise.resolve('http://presigned')
},
}
const a = adapter({
minio: happyMinio,
bucketPrefix: 'test',
region: 'us-east-2',
useNamespacedBucket: false,
})
await t.step('makeBucket', async (t) => {
await t.step('should return whether the bucket was created successfully', () => {
return a.makeBucket('foo')
.then((res) => assertEquals(res, { ok: true }))
})
await t.step('should return a HyperErr if the bucket already exists', () => {
return a.makeBucket('exists')
.then((res) => assertObjectMatch(res, { ok: false, status: 409 }))
})
})
await t.step('removeBucket', async (t) => {
await t.step('should return whether the bucket was removed successfully', () => {
return a.removeBucket('foo')
.then((res) => assertEquals(res, { ok: true }))
})
await t.step('should remove all objects in the bucket before removing', () => {
return a.removeBucket('removeBucketTest')
.then((res) => assertEquals(res, { ok: true }))
})
})
await t.step('listBuckets', async (t) => {
await t.step('should return the names of the buckets', () => {
return a.listBuckets()
.then((res) =>
assertObjectMatch(res, {
ok: true,
buckets: [
'test-foo',
'test-bar',
],
})
)
})
})
await t.step('putObject', async (t) => {
await t.step('should return that the object was put successfully', () => {
return a.putObject({
bucket: 'foo',
object: 'bar.png',
stream: new Response(JSON.stringify({ foo: 'bar' })).body,
})
.then((res) => assertEquals(res, { ok: true }))
})
await t.step('should return the presigned url', () => {
return a.putObject({
bucket: 'foo',
object: 'bar.png',
useSignedUrl: true,
})
.then((res) => assertEquals(res, { ok: true, url: 'http://presigned' }))
})
})
await t.step('removeObject', async (t) => {
await t.step('should return that the object was removed successfully', () => {
return a.removeObject({
bucket: 'foo',
object: 'bar.png',
})
.then((res) => assertEquals(res, { ok: true }))
})
})
await t.step('getObject', async (t) => {
await t.step('should return the stream of the object', () => {
return a.getObject({
bucket: 'foo',
object: 'bar.png',
})
.then(async (res) => {
const object = await (new Response(res).json())
assertEquals(object, { fizz: 'buzz' })
})
})
await t.step('should return the presigned url', () => {
return a.getObject({
bucket: 'foo',
object: 'bar.png',
useSignedUrl: true,
})
.then((res) => assertEquals(res, { ok: true, url: 'http://presigned' }))
})
})
await t.step('listObjects', async (t) => {
await t.step('should return names of objects in the bucket', () => {
return a.listObjects({
bucket: 'foo',
prefix: 'fizz',
})
.then((res) => {
assertEquals(res, { ok: true, objects: ['foo', 'bar'] })
})
})
})
})