|
1
|
|
|
import chai from'chai' |
|
2
|
|
|
import sinonChai from'sinon-chai' |
|
3
|
|
|
var expect = chai.expect |
|
4
|
|
|
chai.use(sinonChai) |
|
5
|
|
|
import sinon from 'sinon' |
|
6
|
|
|
import path from 'path' |
|
7
|
|
|
import {Promise} from 'bluebird' |
|
8
|
|
|
import fse from 'fs-extra' |
|
9
|
|
|
import events from 'events' |
|
10
|
|
|
import mkdirp from 'mkdirp' |
|
11
|
|
|
import { |
|
12
|
|
|
config, |
|
13
|
|
|
cmsMedia, |
|
14
|
|
|
coreUtils, |
|
15
|
|
|
abeExtend |
|
16
|
|
|
} from '../src/cli' |
|
17
|
|
|
config.set({root: path.join(__dirname,'fixtures')}) |
|
18
|
|
|
|
|
19
|
|
|
describe('image', function() { |
|
20
|
|
|
|
|
21
|
|
|
var pathToImage = path.join(config.root, config.publish.url, config.upload.image, 'chat-1.jpg'); |
|
|
|
|
|
|
22
|
|
|
var imagesListInFolder = [ |
|
23
|
|
|
path.join(config.root, config.publish.url, config.upload.image, 'img.jpg'), |
|
24
|
|
|
path.join(config.root, config.publish.url, config.upload.image, 'img_100x100.jpg'), |
|
25
|
|
|
path.join(config.root, config.publish.url, config.upload.image, 'img_10x10.jpg'), |
|
26
|
|
|
path.join(config.root, config.publish.url, config.upload.image, 'img_thumb.jpg') |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
before(function(done) { |
|
30
|
|
|
fse.copySync(path.join(config.root, 'media'), config.root) |
|
31
|
|
|
done() |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* cmsMedia.image.generateThumbnail |
|
36
|
|
|
* @todo : UNIT TEST this method |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
it('cmsMedia.image.generateThumbnail()', function(done) { |
|
40
|
|
|
// this.sinon = sinon.sandbox.create(); |
|
41
|
|
|
// var stub = sinon.stub(cmsMedia.image, 'smartCropAndSaveFile') |
|
42
|
|
|
// stub.returns(Promise.resolve(0)) |
|
43
|
|
|
// var thumb = cmsMedia.image.generateThumbnail(pathToImage) |
|
44
|
|
|
// thumb.then(function (result) { |
|
45
|
|
|
// done() |
|
46
|
|
|
// chai.expect(result).to.not.be.undefined |
|
47
|
|
|
// chai.expect(result.thumb).to.not.be.undefined |
|
48
|
|
|
// chai.expect(result.thumb).to.equal('/unitimage/chat-1_thumb.jpg') |
|
49
|
|
|
// sinon.assert.calledOnce(cmsMedia.image.smartCropAndSaveFile) |
|
50
|
|
|
// cmsMedia.image.smartCropAndSaveFile.restore() |
|
51
|
|
|
// }) |
|
52
|
|
|
done() |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* cmsMedia.image.cropAndSaveFiles |
|
57
|
|
|
* @todo : UNIT TEST this method |
|
58
|
|
|
* |
|
59
|
|
|
*/ |
|
60
|
|
|
it('cmsMedia.image.cropAndSaveFiles()', function(done) { |
|
61
|
|
|
// this.sinon = sinon.sandbox.create(); |
|
62
|
|
|
// var stub = sinon.stub(cmsMedia.image, 'smartCropAndSaveFile') |
|
63
|
|
|
// stub.returns(Promise.resolve({ |
|
64
|
|
|
// 'stdout': false, |
|
65
|
|
|
// 'stderr': false |
|
66
|
|
|
// })) |
|
67
|
|
|
// var thumbs = cmsMedia.image.cropAndSaveFiles(['10x10'], pathToImage, {}) |
|
68
|
|
|
|
|
69
|
|
|
// thumbs.then(function (result) { |
|
70
|
|
|
// console.log("result", result) |
|
71
|
|
|
// chai.expect(result).to.not.be.undefined |
|
72
|
|
|
// chai.expect(result.thumbs).to.have.length(1) |
|
73
|
|
|
// chai.expect(result.thumbs[0]).to.have.property('name').to.equal('/unitimage/chat-1_10x10.jpg') |
|
74
|
|
|
// chai.expect(result.thumbs[0]).to.have.property('size').to.equal('10x10') |
|
75
|
|
|
// sinon.assert.calledOnce(cmsMedia.image.smartCropAndSaveFile) |
|
76
|
|
|
// cmsMedia.image.smartCropAndSaveFile.restore() |
|
77
|
|
|
|
|
78
|
|
|
// done() |
|
79
|
|
|
// }) |
|
80
|
|
|
// .catch(function (err) { |
|
81
|
|
|
// console.log("catch", err) |
|
82
|
|
|
// done() |
|
83
|
|
|
// }) |
|
84
|
|
|
done() |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* cmsMedia.image.getThumbsList |
|
89
|
|
|
* |
|
90
|
|
|
*/ |
|
91
|
|
|
it('cmsMedia.image.getThumbsList()', function() { |
|
92
|
|
|
this.sinon = sinon.sandbox.create(); |
|
93
|
|
|
var stub = sinon.stub(coreUtils.file, 'getFilesSync') |
|
94
|
|
|
stub.returns(imagesListInFolder) |
|
95
|
|
|
var result = cmsMedia.image.getThumbsList() |
|
96
|
|
|
chai.expect(result).to.not.be.undefined |
|
|
|
|
|
|
97
|
|
|
chai.expect(result).to.have.length(1) |
|
98
|
|
|
chai.expect(result[0]).to.have.property('originalFile').to.equal(path.join(path.sep, 'unitimage', 'img.jpg')) |
|
99
|
|
|
chai.expect(result[0]).to.have.property('thumbFile').to.equal(path.join(path.sep, 'unitimage', 'img_thumb.jpg')) |
|
100
|
|
|
sinon.assert.calledOnce(coreUtils.file.getFilesSync) |
|
101
|
|
|
coreUtils.file.getFilesSync.restore() |
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* cmsMedia.image.createMediaFolder |
|
106
|
|
|
* |
|
107
|
|
|
*/ |
|
108
|
|
|
it('cmsMedia.image.createMediaFolder()', function() { |
|
109
|
|
|
this.sinon = sinon.sandbox.create(); |
|
110
|
|
|
var stub = sinon.stub(abeExtend.hooks.instance, 'trigger') |
|
111
|
|
|
stub.returns('/myImageFolder') |
|
112
|
|
|
var stubDir = sinon.stub(mkdirp, 'sync') |
|
113
|
|
|
stubDir.returns({}) |
|
114
|
|
|
var result = cmsMedia.image.createMediaFolder({}) |
|
115
|
|
|
chai.expect(result).to.not.be.undefined |
|
|
|
|
|
|
116
|
|
|
chai.expect(result).to.be.a('string') |
|
117
|
|
|
chai.expect(result).to.equal(path.join(config.root, config.publish.url, '/myImageFolder')) |
|
118
|
|
|
sinon.assert.calledOnce(abeExtend.hooks.instance.trigger) |
|
119
|
|
|
abeExtend.hooks.instance.trigger.restore() |
|
120
|
|
|
sinon.assert.calledOnce(mkdirp.sync) |
|
121
|
|
|
mkdirp.sync.restore() |
|
122
|
|
|
}); |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* cmsMedia.image.getAssociatedImageFileFromThumb |
|
126
|
|
|
* |
|
127
|
|
|
*/ |
|
128
|
|
|
it('cmsMedia.image.getAssociatedImageFileFromThumb()', function() { |
|
129
|
|
|
this.sinon = sinon.sandbox.create(); |
|
130
|
|
|
var stub = sinon.stub(coreUtils.file, 'getFilesSync') |
|
131
|
|
|
stub.returns(imagesListInFolder) |
|
132
|
|
|
var result = cmsMedia.image.getAssociatedImageFileFromThumb(path.join(path.sep, 'unitimage', 'img_thumb.jpg')) |
|
133
|
|
|
chai.expect(result).to.not.be.undefined |
|
|
|
|
|
|
134
|
|
|
chai.expect(result).to.have.property('originalFile').to.equal(path.join(path.sep, 'unitimage', 'img.jpg')) |
|
135
|
|
|
chai.expect(result).to.have.property('thumbFile').to.equal(path.join(path.sep, 'unitimage', 'img_thumb.jpg')) |
|
136
|
|
|
chai.expect(result.thumbs).to.have.length(2) |
|
137
|
|
|
chai.expect(result.thumbs[0]).to.equal(path.join(path.sep, 'unitimage', 'img_100x100.jpg')) |
|
138
|
|
|
sinon.assert.calledOnce(coreUtils.file.getFilesSync) |
|
139
|
|
|
coreUtils.file.getFilesSync.restore() |
|
140
|
|
|
}); |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* cmsMedia.image.isValidMedia |
|
144
|
|
|
* |
|
145
|
|
|
*/ |
|
146
|
|
|
it('cmsMedia.image.isValidMedia()', function() { |
|
147
|
|
|
var result = cmsMedia.image.isValidMedia('image/jpeg', '.jpg') |
|
148
|
|
|
var result2 = cmsMedia.image.isValidMedia('wrong/mimetype', '.exe') |
|
149
|
|
|
chai.expect(result.error).to.equal(false) |
|
150
|
|
|
chai.expect(result2.error).to.be.a('string') |
|
151
|
|
|
chai.expect(result2.error).to.equal('unauthorized file') |
|
152
|
|
|
}); |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* cmsMedia.image.createMediaSlug |
|
156
|
|
|
* |
|
157
|
|
|
*/ |
|
158
|
|
|
it('cmsMedia.image.createMediaSlug()', function() { |
|
159
|
|
|
this.sinon = sinon.sandbox.create(); |
|
160
|
|
|
var stub = sinon.stub(coreUtils.random, 'generateUniqueIdentifier') |
|
161
|
|
|
stub.returns(12345) |
|
162
|
|
|
var result = cmsMedia.image.createMediaSlug('teSt f1le s l û g', '.jpg') |
|
163
|
|
|
chai.expect(result).to.not.be.undefined |
|
|
|
|
|
|
164
|
|
|
chai.expect(result).to.be.a('string') |
|
165
|
|
|
chai.expect(result).to.equal('test-f1le-s-l-u-g-12345.jpg') |
|
166
|
|
|
sinon.assert.calledOnce(coreUtils.random.generateUniqueIdentifier) |
|
167
|
|
|
coreUtils.random.generateUniqueIdentifier.restore() |
|
168
|
|
|
}); |
|
169
|
|
|
|
|
170
|
|
|
after(function(done) { |
|
171
|
|
|
fse.remove(path.join(config.root, config.publish.url)) |
|
172
|
|
|
done() |
|
173
|
|
|
}); |
|
174
|
|
|
|
|
175
|
|
|
}); |