@@ 15-123 (lines=109) @@ | ||
12 | chai.use(chaiaspromised); |
|
13 | chai.use(sinonchai); |
|
14 | ||
15 | describe(`[module] codingame/api`, function() { |
|
16 | describe(`[method] login`, function() { |
|
17 | let credentialsok = { |
|
18 | "username": `me`, |
|
19 | "password": `p4ssw0rd` |
|
20 | }; |
|
21 | let credentialsnotok = { |
|
22 | "username": `me`, |
|
23 | "password": `password` |
|
24 | }; |
|
25 | let response = { |
|
26 | "success": true |
|
27 | }; |
|
28 | before(function() { |
|
29 | nock(`https://www.codingame.com`) |
|
30 | .post(`/services/CodingamerRemoteService/loginSiteV2`, [credentialsok.username, credentialsok.password, true]) |
|
31 | .reply(200, response) |
|
32 | .post(`/services/CodingamerRemoteService/loginSiteV2`, [credentialsnotok.username, credentialsnotok.password, true]) |
|
33 | .reply(403); |
|
34 | }); |
|
35 | after(function() { |
|
36 | nock.cleanAll(); |
|
37 | }); |
|
38 | it(`should resolve with correct username and password`, function() { |
|
39 | let login = cgapi.login(credentialsok.username, credentialsok.password); |
|
40 | return expect(login).to.be.fulfilled; |
|
41 | }); |
|
42 | it(`should reject with incorrect password`, function() { |
|
43 | let login = cgapi.login(credentialsnotok.username, credentialsnotok.password); |
|
44 | return expect(login).to.be.rejected; |
|
45 | }); |
|
46 | }); |
|
47 | describe(`[method] test`, function() { |
|
48 | let exercise = `5711567e959cf54dd2dd79c1b4c259560d6ba46`; |
|
49 | let bundle = `print('1')`; |
|
50 | let expected = `1`; |
|
51 | let found = `-`; |
|
52 | let language = `Python`; |
|
53 | let testindex = 1; |
|
54 | let body = [ |
|
55 | exercise, |
|
56 | { |
|
57 | "code": bundle, |
|
58 | "programmingLanguageId": language, |
|
59 | "multipleLanguages": { |
|
60 | "testIndex": testindex |
|
61 | } |
|
62 | } |
|
63 | ]; |
|
64 | let meta = { |
|
65 | "exercise": exercise, |
|
66 | "test": testindex, |
|
67 | "language": language, |
|
68 | "bundle": bundle |
|
69 | }; |
|
70 | let sandbox; |
|
71 | beforeEach(function() { |
|
72 | sandbox = sinon.sandbox.create(); |
|
73 | }); |
|
74 | afterEach(function() { |
|
75 | sandbox.restore(); |
|
76 | nock.cleanAll(); |
|
77 | }); |
|
78 | it(`should resolve with metadata if test has succeeded`, function() { |
|
79 | let parse = sandbox.stub(cgparse, `parse`).callsFake(function() { |
|
80 | return Promise.resolve(meta); |
|
81 | }) |
|
82 | nock(`https://www.codingame.com`) |
|
83 | .post(`/services/TestSessionRemoteService/play`, body) |
|
84 | .reply(200, {}); |
|
85 | let test = cgapi.test(exercise, testindex, language, bundle); |
|
86 | return expect(test).to.be.fulfilled |
|
87 | .and.to.eventually.be.deep.equal(meta); |
|
88 | }); |
|
89 | it(`should reject with CodingameError if response is ok but test failed`, function() { |
|
90 | let message = `Error message`; |
|
91 | let parse = sandbox.stub(cgparse, `parse`).callsFake(function() { |
|
92 | let error = new CodingameError(message); |
|
93 | return Promise.reject(error); |
|
94 | }) |
|
95 | nock(`https://www.codingame.com`) |
|
96 | .post(`/services/TestSessionRemoteService/play`, body) |
|
97 | .reply(200, {}); |
|
98 | let test = cgapi.test(exercise, testindex, language, bundle); |
|
99 | return expect(test).to.be.rejected |
|
100 | .and.to.eventually.be.an.instanceof(CodingameError); |
|
101 | }); |
|
102 | it(`should reject with Error if parsing failed`, function() { |
|
103 | let message = `Error message`; |
|
104 | let parse = sandbox.stub(cgparse, `parse`).callsFake(function() { |
|
105 | let error = new Error(message); |
|
106 | return Promise.reject(error); |
|
107 | }) |
|
108 | nock(`https://www.codingame.com`) |
|
109 | .post(`/services/TestSessionRemoteService/play`, body) |
|
110 | .reply(200, {}); |
|
111 | let test = cgapi.test(exercise, testindex, language, bundle); |
|
112 | return expect(test).to.be.rejected |
|
113 | .and.to.eventually.be.an.instanceof(Error); |
|
114 | }); |
|
115 | it(`should reject because server returned an HTTP error code`, function() { |
|
116 | nock(`https://www.codingame.com`) |
|
117 | .post(`/services/TestSessionRemoteService/play`, body) |
|
118 | .reply(403, {}); |
|
119 | let test = cgapi.test(exercise, testindex, language, bundle); |
|
120 | return expect(test).to.be.rejected; |
|
121 | }); |
|
122 | }); |
|
123 | }); |
|
124 |
@@ 63-154 (lines=92) @@ | ||
60 | return expect(conf).to.eventually.be.deep.equal(defaultconf); |
|
61 | }); |
|
62 | }); |
|
63 | describe(`[method] get`, function() { |
|
64 | let filepath = `file.txt`; |
|
65 | let filecontent = `Hello world!`; |
|
66 | let shellcmd = [`echo`, filecontent]; |
|
67 | let defaultconf = { |
|
68 | "shell": shellcmd, |
|
69 | "wrongshell": [`notacommand`], |
|
70 | "path": filepath, |
|
71 | "notapath": `/not/a/path` |
|
72 | }; |
|
73 | let answer = `42`; |
|
74 | before(function(done) { |
|
75 | mockfs({ |
|
76 | [filepath]: filecontent |
|
77 | }); |
|
78 | configure.load(undefined, defaultconf).then(function() { |
|
79 | done(); |
|
80 | }); |
|
81 | }); |
|
82 | after(function() { |
|
83 | mockfs.restore(); |
|
84 | for (let param in defaultconf) { |
|
85 | configure.forget(param); |
|
86 | } |
|
87 | }); |
|
88 | let sandbox; |
|
89 | let createInterface; |
|
90 | beforeEach(function() { |
|
91 | sandbox = sinon.sandbox.create(); |
|
92 | createInterface = sandbox.stub(readline, `createInterface`).callsFake(function() { |
|
93 | return { |
|
94 | "question": function(question, cb) { |
|
95 | cb(answer); |
|
96 | }, |
|
97 | "close": function() {} |
|
98 | }; |
|
99 | }); |
|
100 | }); |
|
101 | afterEach(function() { |
|
102 | sandbox.restore(); |
|
103 | }); |
|
104 | it(`should return the string property as it is`, function() { |
|
105 | let get = configure.get(`path`); |
|
106 | return expect(get).to.eventually.be.equal(filepath); |
|
107 | }); |
|
108 | it(`should return the array property as it is`, function() { |
|
109 | let get = configure.get(`shell`); |
|
110 | return expect(get).to.eventually.be.deep.equal(shellcmd); |
|
111 | }); |
|
112 | it(`should return the result of the shell command`, function() { |
|
113 | let get = configure.get(`shell`, `shell`); |
|
114 | return expect(get).to.eventually.be.equal(filecontent); |
|
115 | }); |
|
116 | it(`should return the content of the file`, function() { |
|
117 | let get = configure.get(`path`, `file`); |
|
118 | return expect(get).to.eventually.be.deep.equal({ |
|
119 | "path": filepath, |
|
120 | "data": filecontent |
|
121 | }); |
|
122 | }); |
|
123 | it(`should return answer to the question`, function() { |
|
124 | let ask = `ask something?`; |
|
125 | let get = configure.get(`questionproperty`, ``, ask); |
|
126 | expect(createInterface).to.be.calledOnce; |
|
127 | return expect(get).to.eventually.be.equal(answer); |
|
128 | }); |
|
129 | it(`should reject because property doesn't exist`, function() { |
|
130 | let get = configure.get(`notaproperty`); |
|
131 | return expect(get).to.be.rejectedWith(Error); |
|
132 | }); |
|
133 | it(`should reject if first parameter is not a string`, function() { |
|
134 | let get = configure.get(42); |
|
135 | return expect(get).to.be.rejectedWith(Error); |
|
136 | }); |
|
137 | it(`should reject if shell command is failing`, function() { |
|
138 | let unmute = mute(process.stderr); |
|
139 | let get = configure.get(`wrongshell`, `shell`); |
|
140 | get.catch(unmute); |
|
141 | return expect(get).to.be.rejectedWith(Error); |
|
142 | }); |
|
143 | it(`should reject if file doesn't exist`, function() { |
|
144 | let unmute = mute(process.stderr); |
|
145 | let get = configure.get(`notapath`, `file`); |
|
146 | get.catch(unmute); |
|
147 | return expect(get).to.be.rejectedWith(Error); |
|
148 | }); |
|
149 | it(`should reject if question is not a string`, function() { |
|
150 | let get = configure.get(`incorrectquestionproperty`, ``, 42); |
|
151 | expect(createInterface).to.be.calledOnce; |
|
152 | return expect(get).to.be.rejectedWith(Error); |
|
153 | }); |
|
154 | }); |
|
155 | describe(`[method] forget`, function() { |
|
156 | let conf = { |
|
157 | "property": `value` |