-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_upload_post_mocha.js
105 lines (94 loc) · 4.29 KB
/
files_upload_post_mocha.js
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
var should = require('chai').should();
var fs = require('fs');
var request = require('request');
// start mocha test
describe('When performing a slack file upload test,', function() {
// setup shared test data object
var testData = {
url_download: null,
thumb_64: null,
thumb_80: null,
thumb_360: null,
sizeOriginal: null,
size64: null,
size80: null,
size360: null,
}
it('should upload png and verify file object response', function(done) {
// setup test variables
var apiURL = 'https://slack.com/api';
var apiEndpoint = '/files.upload';
var accessToken = 'ENTER ACCESS TOKEN HERE';
var fileName = 'APPLE_IPHONE.PNG';
var urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
var originalRegex = new RegExp(fileName.toLowerCase(), 'g');
var thumb64Regex = new RegExp(fileName.toLowerCase().replace('.png', '_64.png'), 'g');
var thumb80Regex = new RegExp(fileName.toLowerCase().replace('.png', '_80.png'), 'g');
var thumb360Regex = new RegExp(fileName.toLowerCase().replace('.png', '_360.png'), 'g');
// make POST call to upload sample file
request.post(apiURL + apiEndpoint + '?token=' + accessToken, function optionalCallback (err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
// make assertions on file object
httpResponse.statusCode.should.equal(200);
var body = JSON.parse(body);
body.should.be.an('object');
body.ok.should.be.true;
body.file.should.have.property('id').and.to.be.a('string').and.to.not.be.empty;
body.file.should.have.property('url_download').and.to.be.a('string').and.to.match(urlRegex).and.to.match(originalRegex);
body.file.should.have.property('thumb_64').and.to.be.a('string').and.to.match(urlRegex).and.to.match(thumb64Regex);
body.file.should.have.property('thumb_80').and.to.be.a('string').and.to.match(urlRegex).and.to.match(thumb80Regex);
body.file.should.have.property('thumb_360').and.to.be.a('string').and.to.match(urlRegex).and.to.match(thumb360Regex);
// save data for future test use
testData.url_download = body.file.url_download;
testData.thumb_64 = body.file.thumb_64;
testData.thumb_80 = body.file.thumb_80;
testData.thumb_360 = body.file.thumb_360;
done();
})
.form()
.append('file', fs.createReadStream(fileName));
});
// download thumbnails and check their respective lengths are within range
it('should download original image and verify expected length', function(done) {
request(testData.url_download, function(error, response, body) {
body.should.not.be.empty;
body.should.have.length.within(648017,648021);
testData.sizeOriginal = body.length;
done();
});
});
it('should download 64 thumbnail and verify expected length', function(done) {
request(testData.thumb_64, function(error, response, body) {
body.should.not.be.empty;
body.should.have.length.within(9272,9276);
testData.size64 = body.length;
done();
});
});
it('should download 80 thumbnail and verify expected length', function(done) {
request(testData.thumb_80, function(error, response, body) {
body.should.not.be.empty;
body.should.have.length.within(13544,13548);
testData.size80 = body.length;
done();
});
});
it('should download 360 thumbnail and verify expected length', function(done) {
request(testData.thumb_360, function(error, response, body) {
body.should.not.be.empty;
body.should.have.length.within(123633,123637);
testData.size360 = body.length;
done();
});
});
// confirm that the thumbnail file sizes are an expected hierarchy of length
it('should confirm file sizes correspond to thumbnail size by length', function(done) {
testData.sizeOriginal.should.be.above(testData.size360).and.be.above(testData.size80).and.be.above(testData.size64);
testData.size360.should.be.below(testData.sizeOriginal).and.be.above(testData.size80).and.be.above(testData.size64);
testData.size80.should.be.below(testData.sizeOriginal).and.be.below(testData.size360).and.be.above(testData.size64);
testData.size64.should.be.below(testData.sizeOriginal).and.be.below(testData.size360).and.be.below(testData.size80);
done();
});
});