@@ -7,106 +7,64 @@ const fs = require('fs')
7
7
const path = require ( 'path' )
8
8
const uuid = require ( 'uuid/v4' )
9
9
const chai = require ( 'chai' )
10
- const service = require ( '../../src/services/AttachmentService ' )
10
+ const awsMock = require ( 'aws-sdk-mock ' )
11
11
const testHelper = require ( '../testHelper' )
12
+ const prisma = require ( '../../src/common/prisma' ) . getClient ( )
12
13
13
14
const should = chai . should ( )
14
15
15
16
const attachmentContent = fs . readFileSync ( path . join ( __dirname , '../attachment.txt' ) )
16
17
17
- /*
18
+ let service
19
+
18
20
describe ( 'attachment service unit tests' , ( ) => {
19
21
// created attachment id
20
22
let id
21
23
// generated data
22
24
let data
25
+ // attachment for task challenge
26
+ let id2
23
27
const notFoundId = uuid ( )
24
28
25
29
before ( async ( ) => {
30
+ // mock S3 before creating S3 instance
31
+ awsMock . mock ( 'S3' , 'getObject' , ( params , callback ) => {
32
+ callback ( null , { Body : Buffer . from ( attachmentContent ) } ) ;
33
+ } ) ;
34
+ // import service after setting up S3 mock
35
+ service = require ( '../../src/services/AttachmentService' )
26
36
await testHelper . createData ( )
27
37
data = testHelper . getData ( )
28
- })
29
-
30
- after(async () => {
31
- await testHelper.clearData()
32
- })
33
-
34
- describe('upload attachment tests', () => {
35
- it('upload attachment successfully', async () => {
36
- const result = await service.uploadAttachment({
37
- isMachine: true
38
- }, data.challenge.id, {
39
- attachment: {
40
- data: attachmentContent,
41
- mimetype: 'text/plain',
42
- name: 'attachment.txt',
43
- size: attachmentContent.length
44
- }
45
- })
46
- should.exist(result.id)
47
- id = result.id
48
- should.equal(result.fileSize, attachmentContent.length)
49
- should.equal(result.fileName, 'attachment.txt')
50
- should.equal(result.challengeId, data.challenge.id)
51
- })
52
-
53
- it('upload attachment - forbidden', async () => {
54
- try {
55
- await service.uploadAttachment({
56
- roles: ['user']
57
- }, data.challenge.id, {
58
- attachment: {
59
- data: attachmentContent,
60
- mimetype: 'text/plain',
61
- name: 'attachment.txt',
62
- size: attachmentContent.length
63
- }
64
- })
65
- } catch (e) {
66
- should.equal(e.message, 'You are not allowed to upload attachment of the challenge.')
67
- return
38
+ // create attachment
39
+ const createdAttachment = await prisma . attachment . create ( {
40
+ data : {
41
+ name : 'attachment.txt' ,
42
+ url : 'http://s3.amazonaws.com/topcoder_01/attachment.txt' ,
43
+ fileSize : 1024 ,
44
+ createdBy : 'testdata' ,
45
+ updatedBy : 'testdata' ,
46
+ challenge : { connect : { id : data . challenge . id } }
68
47
}
69
- throw new Error('should not reach here')
70
48
} )
71
-
72
- it('upload attachment - file too large', async () => {
73
- try {
74
- await service.uploadAttachment({
75
- isMachine: true
76
- }, data.challenge.id, {
77
- attachment: {
78
- truncated: true,
79
- data: attachmentContent,
80
- mimetype: 'text/plain',
81
- name: 'attachment.txt',
82
- size: attachmentContent.length
83
- }
84
- })
85
- } catch (e) {
86
- should.equal(e.message.indexOf('attachment is too large') >= 0, true)
87
- return
49
+ id = createdAttachment . id
50
+ const taskAttachment = await prisma . attachment . create ( {
51
+ data : {
52
+ name : 'attachment.txt' ,
53
+ url : 'http://s3.amazonaws.com/topcoder_01/attachment.txt' ,
54
+ fileSize : 1024 ,
55
+ createdBy : 'testdata' ,
56
+ updatedBy : 'testdata' ,
57
+ challenge : { connect : { id : data . taskChallenge . id } }
88
58
}
89
- throw new Error('should not reach here')
90
59
} )
60
+ id2 = taskAttachment . id
61
+ } )
91
62
92
- it('upload attachment - challenge not found', async () => {
93
- try {
94
- await service.uploadAttachment({
95
- isMachine: true
96
- }, notFoundId, {
97
- attachment: {
98
- data: attachmentContent,
99
- mimetype: 'text/plain',
100
- name: 'attachment.txt',
101
- size: attachmentContent.length
102
- }
103
- })
104
- } catch (e) {
105
- should.equal(e.message, `Challenge with id: ${notFoundId} doesn't exist`)
106
- return
107
- }
108
- throw new Error('should not reach here')
109
- })
63
+ after ( async ( ) => {
64
+ await testHelper . clearData ( )
65
+ await prisma . attachment . deleteMany ( { where : { id } } )
66
+ // restore S3
67
+ awsMock . restore ( 'S3' ) ;
110
68
} )
111
69
112
70
describe ( 'download attachment tests' , ( ) => {
@@ -118,9 +76,9 @@ describe('attachment service unit tests', () => {
118
76
119
77
it ( 'download attachment - forbidden' , async ( ) => {
120
78
try {
121
- await service.downloadAttachment({ roles: ['user'], userId: 678678 }, data.challenge .id, id )
79
+ await service . downloadAttachment ( { roles : [ 'user' ] , userId : 678678 } , data . taskChallenge . id , id2 )
122
80
} catch ( e ) {
123
- should.equal(e.message, 'You are not allowed to download attachment of the challenge. ')
81
+ should . equal ( e . message , 'You don\'t have access to view this challenge' )
124
82
return
125
83
}
126
84
throw new Error ( 'should not reach here' )
@@ -130,7 +88,7 @@ describe('attachment service unit tests', () => {
130
88
try {
131
89
await service . downloadAttachment ( { isMachine : true } , data . challenge . id , notFoundId )
132
90
} catch ( e ) {
133
- should.equal(e.message, `Attachment with id: ${notFoundId} doesn't exist `)
91
+ should . equal ( e . message , `Attachment ${ notFoundId } not found in challenge ${ data . challenge . id } ` )
134
92
return
135
93
}
136
94
throw new Error ( 'should not reach here' )
@@ -140,7 +98,7 @@ describe('attachment service unit tests', () => {
140
98
try {
141
99
await service . downloadAttachment ( { isMachine : true } , notFoundId , id )
142
100
} catch ( e ) {
143
- should.equal(e.message, 'The attachment challengeId does not match the path challengeId.' )
101
+ should . equal ( e . message , `Attachment ${ id } not found in challenge ${ notFoundId } ` )
144
102
return
145
103
}
146
104
throw new Error ( 'should not reach here' )
@@ -167,5 +125,3 @@ describe('attachment service unit tests', () => {
167
125
} )
168
126
} )
169
127
} )
170
-
171
- */
0 commit comments