blob: b4c4bd66d7843f7ae1731846406858b4013c5d38 [file] [log] [blame]
Michael Lando451a3402017-02-19 10:28:42 +02001describe("General", function() {
2
3
4
5 describe("File Handler", function() {
6
7 var jsHandler = new JsHandler();
8 var fileHandler = jsHandler.getFileHandler();
9 fileHandler.fileContents.fileToUpload = mysqlTypeYml;
10 fileHandler.fileContents.artifactToUpload = installMySqlSH;
11
12
13 describe("checkFile Method", function() {
14
15 it("checkFile Valid", function() {
16 spyOn(window, 'alert');
17
18 affix('#dummyElement').val('stam.zip');
19 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.chef, 'dummyElement');
20 expect(isValid).toBeTruthy();
21
22
23 $('#dummyElement').val('stam.sh');
24 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.shell, 'dummyElement');
25 expect(isValid).toBeTruthy();
26
27 $('#dummyElement').val('stam.pp');
28 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.puppet, 'dummyElement');
29 expect(isValid).toBeTruthy();
30
31 $('#dummyElement').val('stam.yang');
32 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.yang, 'dummyElement');
33 expect(isValid).toBeTruthy();
34
35
36
37 });
38
39 it("checkFile inValid", function() {
40 spyOn(window, 'alert');
41
42 affix('#dummyElement').val('stam.zip');
43 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.shell, 'dummyElement');
44 expect(isValid).toBeFalsy();
45
46 $('#dummyElement').val('stam.sh');
47 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.chef, 'dummyElement');
48 expect(isValid).toBeFalsy();
49
50 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.puppet, 'dummyElement');
51 expect(isValid).toBeFalsy();
52
53 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.yang, 'dummyElement');
54 expect(isValid).toBeFalsy();
55
56 });
57
58 });
59
60
61
62 describe("getZipName Method", function() {
63 it("getZipName", function() {
64 spyOn(fileHandler, 'getFileName').and.returnValue('mysql-type.yml');
65 expect(fileHandler.getZipName() == 'mysql-type.zip').toBeTruthy();
66 });
67 });
68 /*
69 describe("generateArtifactsList Method", function() {
70 beforeEach(function() {
71 jsHandler.getDomHandler().exisitngArifactsCounter = 2;
72 });
73
74 it("generateArtifactsList", function() {
75 spyOn(fileHandler, 'getFileName').and.returnValue('mysql-type.yml');
76 var artifactList = fileHandler.generateArtifactsList();
77// expect(fileHandler.generateArtifactsList() == 'mysql-type.zip').toBeTruthy();
78 });
79 });*/
80
81 describe("getArtifactsListFromYml Method", function() {
82 it("getArtifactsListFromYml", function() {
83 var expectedArtifacts = fileHandler.getArtifactsListFromYml();
84 expect(expectedArtifacts.length == 3).toBeTruthy();
85 expect($.inArray('scripts/install_mysql.sh', expectedArtifacts) != -1).toBeTruthy();
86 expect($.inArray('scripts/start_mysql.sh', expectedArtifacts) != -1).toBeTruthy();
87 expect($.inArray('images/mysql.png', expectedArtifacts) != -1).toBeTruthy();
88 });
89 });
90
91 describe("uploadFileJson Method", function() {
92 var sentJson, sendEncodedMd5;
93
94 beforeEach(function() {
95 sentJson = '';
96 sendEncodedMd5 = '';
97 spyOn(fileHandler, 'sendAjaxToServer').and.callFake(function(strifiedJson, encodedMd5) {
98 sentJson = JSON.parse(strifiedJson);
99 sendEncodedMd5 = encodedMd5;
100
101 });
102 spyOn(fileHandler, 'getZipName').and.callFake(function() { return 'fakeZipName.zip' });
103 spyOn(fileHandler, 'generateArtifactsList').and.callFake(function() { return 'fakeArtifactsList' });
104 spyOn(fileHandler, 'generateZip').and.callFake(function() { return 'fakeEncodedZipData' });
105 });
106
107 it("sendAjaxToServer method is called", function() {
108 fileHandler.uploadFileJson();
109 expect(fileHandler.sendAjaxToServer).toHaveBeenCalled();
110 });
111
112 it("json contains relevant fields", function() {
113 fileHandler.uploadFileJson();
114
115 expect(sentJson.payloadName == 'fakeZipName.zip').toBeTruthy();
116 expect(sentJson.isEncoded).toBeTruthy();
117 expect(sentJson.isCompressed).toBeTruthy();
118 expect(sentJson.artifactList == 'fakeArtifactsList').toBeTruthy();
119 expect(sentJson.payloadData == 'fakeEncodedZipData').toBeTruthy();
120
121
122
123 });
124
125 it("md5 is validated", function() {
126 fileHandler.uploadFileJson();
127
128 var strifiedJson = JSON.stringify(sentJson);
129 var md5 = CryptoJS.MD5(strifiedJson);
130 var encodedMd5 = btoa(md5);
131 expect(encodedMd5 == sendEncodedMd5).toBeTruthy();
132
133 });
134 });
135
136 describe("generateZip Method", function() {
137
138 it("generateZip", function() {
139
140
141 spyOn(fileHandler, 'getFileName').and.callFake(function(controName) {
142 var fileName;
143 if( controName == 'fileToUpload'){
144 fileName = 'mysql-type.yml';
145 }
146 else if( controName == 'imageToUpload'){
147 fileName = 'root.png';
148 }
149 else if( controName == 'artifactToUpload'){
150 fileName = 'install_mysql.sh';
151 }
152 else{
153 fileName = controName+'.sh';
154 }
155 return fileName;
156 });
157
158 var encodedZip = fileHandler.generateZip();
159 var decodedZip = atob(encodedZip);
160 var myZip = new JSZip(decodedZip);
161
162 var componentContentInZip = myZip.file('mysql-type.yml').asText();
163 expect(componentContentInZip == mysqlTypeYml).toBeTruthy();
164
165 var artifactContentInZip = myZip.folder('scripts').file('install_mysql.sh').asText();
166 expect(artifactContentInZip == installMySqlSH).toBeTruthy();
167
168
169
170 });
171 });
172 });
173
174
175});
176
177