/
analysesClass.submitSourcecode.spec.ts
93 lines (76 loc) · 2.68 KB
/
analysesClass.submitSourcecode.spec.ts
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
import { expect } from 'chai'
import * as sinon from 'sinon'
import * as jwt from 'jsonwebtoken'
import { ClientService } from '../apiServices/ClientService'
import { AnalysesService } from '../apiServices/AnalysesService'
import { JwtTokensInterface } from '..'
const postRequest = require('../http/index')
describe('submitSourceCode', () => {
const accessToken = {
jti: '',
iss: '',
exp: Math.floor(new Date().getTime() / 1000) + 60 * 20,
userId: '',
iat: 0,
}
const tokens: JwtTokensInterface = {
access: jwt.sign(accessToken, 'secret'),
refresh: 'refresh',
}
let postRequestStub: any
let ANALYSES
beforeEach(() => {
postRequestStub = sinon.stub(postRequest, 'postRequest')
ANALYSES = new AnalysesService(tokens, 'MythXJTest')
})
afterEach(() => {
postRequestStub.restore()
})
it('is a function', () => {
expect(ANALYSES.submitSourceCode).to.be.a('function')
})
it('should return an object with info about submitted analysis using source code only', async () => {
const sourceCode = 'solidity code'
const response = {
apiVersion: 'v1.4.14',
harveyVersion: '0.0.22',
maestroVersion: '1.2.11',
maruVersion: '0.4.6',
mythrilVersion: '0.20.4',
queueTime: 10,
runTime: 0,
status: 'Queued',
submittedAt: '2019-05-29T17:41:46.902Z',
submittedBy: '123456789012345678901234',
uuid: '1111-2222-3333-4444',
}
const expected = {
clientToolName: 'MythXJTest',
data: {
contractName: 'contractName',
sources: {
[`${'contractName'}.sol`]: {
source: sourceCode,
},
},
mainSource: `${'contractName'}.sol`,
},
}
postRequestStub.resolves({
data: response,
})
const result = await ANALYSES.submitSourceCode(sourceCode, 'contractName')
expect(result).to.equal(response)
expect(postRequestStub.calledWith(`${ClientService.MYTHX_API_ENVIRONMENT}/analyses`, expected)).to.be.true
})
it('should fail if there is something wrong with the request', async () => {
const sourceCode = 'source code'
postRequestStub.throws('400')
try {
await ANALYSES.submitSourceCode(sourceCode, 'contractName')
expect.fail('submitSourceCode should be rejected')
} catch (err) {
expect(err.message).to.equal('MythxJS. Error with your request. 400')
}
})
})