2020-08-24 17:18:08 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2020-08-27 18:27:56 -07:00
|
|
|
import { MojangRestAPI } from 'common/mojang/rest/MojangRestAPI'
|
2020-04-13 19:21:48 -07:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import nock from 'nock'
|
2020-08-27 18:27:56 -07:00
|
|
|
import { Session } from 'common/mojang/rest/Auth'
|
|
|
|
import { MojangErrorCode, MojangResponse } from 'common/mojang/rest/internal/MojangResponse'
|
2020-08-24 17:18:08 -07:00
|
|
|
import { RestResponseStatus, RestResponse } from 'common/got/RestResponse'
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-08-24 17:18:08 -07:00
|
|
|
function assertResponse(res: RestResponse<unknown>) {
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res).to.not.be.an('error')
|
|
|
|
expect(res).to.be.an('object')
|
2020-08-24 17:18:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectSuccess(res: RestResponse<unknown>) {
|
|
|
|
assertResponse(res)
|
|
|
|
expect(res).to.have.property('responseStatus')
|
|
|
|
expect(res.responseStatus).to.equal(RestResponseStatus.SUCCESS)
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectFailure(res: RestResponse<unknown>) {
|
|
|
|
expect(res.responseStatus).to.not.equal(RestResponseStatus.SUCCESS)
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectMojangResponse(res: MojangResponse<unknown>, responseCode: MojangErrorCode, negate = false) {
|
|
|
|
assertResponse(res)
|
|
|
|
expect(res).to.have.property('mojangErrorCode')
|
2020-04-13 19:21:48 -07:00
|
|
|
if(!negate) {
|
2020-08-24 17:18:08 -07:00
|
|
|
expect(res.mojangErrorCode).to.equal(responseCode)
|
2020-04-13 19:21:48 -07:00
|
|
|
} else {
|
2020-08-24 17:18:08 -07:00
|
|
|
expect(res.mojangErrorCode).to.not.equal(responseCode)
|
2020-04-13 19:21:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:55:24 -07:00
|
|
|
describe('[Mojang Rest API] Errors', () => {
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-04-17 20:50:18 -07:00
|
|
|
after(() => {
|
|
|
|
nock.cleanAll()
|
|
|
|
})
|
|
|
|
|
2020-04-13 19:21:48 -07:00
|
|
|
it('Status (Offline)', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const defStatusHack = MojangRestAPI['statuses']
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.STATUS_ENDPOINT)
|
2020-04-17 20:50:18 -07:00
|
|
|
.get('/check')
|
2020-04-13 19:21:48 -07:00
|
|
|
.reply(500, 'Service temprarily offline.')
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.status()
|
2020-08-24 17:18:08 -07:00
|
|
|
expectFailure(res)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res.data).to.be.an('array')
|
|
|
|
expect(res.data).to.deep.equal(defStatusHack)
|
|
|
|
|
|
|
|
}).timeout(2500)
|
|
|
|
|
|
|
|
it('Authenticate (Invalid Credentials)', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.AUTH_ENDPOINT)
|
2020-04-13 19:21:48 -07:00
|
|
|
.post('/authenticate')
|
2020-08-24 17:18:08 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
.reply(403, (uri, requestBody: unknown): { error: string, errorMessage: string } => {
|
2020-04-13 19:21:48 -07:00
|
|
|
return {
|
|
|
|
error: 'ForbiddenOperationException',
|
|
|
|
errorMessage: 'Invalid credentials. Invalid username or password.'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.authenticate('user', 'pass', 'xxx', true)
|
2020-08-24 17:18:08 -07:00
|
|
|
expectMojangResponse(res, MojangErrorCode.ERROR_INVALID_CREDENTIALS)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res.data).to.be.a('null')
|
|
|
|
expect(res.error).to.not.be.a('null')
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-27 18:55:24 -07:00
|
|
|
describe('[Mojang Rest API] Status', () => {
|
2020-04-13 19:21:48 -07:00
|
|
|
|
|
|
|
it('Status (Online)', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const defStatusHack = MojangRestAPI['statuses']
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.STATUS_ENDPOINT)
|
2020-04-17 20:50:18 -07:00
|
|
|
.get('/check')
|
2020-04-13 19:21:48 -07:00
|
|
|
.reply(200, defStatusHack)
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.status()
|
2020-08-24 17:18:08 -07:00
|
|
|
expectSuccess(res)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res.data).to.be.an('array')
|
|
|
|
expect(res.data).to.deep.equal(defStatusHack)
|
|
|
|
|
|
|
|
}).timeout(2500)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2020-08-27 18:55:24 -07:00
|
|
|
describe('[Mojang Rest API] Auth', () => {
|
2020-04-13 19:21:48 -07:00
|
|
|
|
|
|
|
it('Authenticate', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.AUTH_ENDPOINT)
|
2020-04-13 19:21:48 -07:00
|
|
|
.post('/authenticate')
|
|
|
|
.reply(200, (uri, requestBody: any): Session => {
|
|
|
|
const mockResponse: Session = {
|
|
|
|
accessToken: 'abc',
|
|
|
|
clientToken: requestBody.clientToken,
|
|
|
|
selectedProfile: {
|
|
|
|
id: 'def',
|
|
|
|
name: 'username'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(requestBody.requestUser) {
|
|
|
|
mockResponse.user = {
|
|
|
|
id: 'def',
|
|
|
|
properties: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mockResponse
|
|
|
|
})
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.authenticate('user', 'pass', 'xxx', true)
|
2020-08-24 17:18:08 -07:00
|
|
|
expectSuccess(res)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res.data!.clientToken).to.equal('xxx')
|
|
|
|
expect(res.data).to.have.property('user')
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Validate', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.AUTH_ENDPOINT)
|
2020-04-13 19:21:48 -07:00
|
|
|
.post('/validate')
|
|
|
|
.times(2)
|
|
|
|
.reply((uri, requestBody: any) => {
|
|
|
|
return [
|
|
|
|
requestBody.accessToken === 'abc' ? 204 : 403
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.validate('abc', 'def')
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-08-24 17:18:08 -07:00
|
|
|
expectSuccess(res)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res.data).to.be.a('boolean')
|
|
|
|
expect(res.data).to.equal(true)
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res2 = await MojangRestAPI.validate('def', 'def')
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-08-24 17:18:08 -07:00
|
|
|
expectSuccess(res2)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res2.data).to.be.a('boolean')
|
|
|
|
expect(res2.data).to.equal(false)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Invalidate', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.AUTH_ENDPOINT)
|
2020-04-13 19:21:48 -07:00
|
|
|
.post('/invalidate')
|
|
|
|
.reply(204)
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.invalidate('adc', 'def')
|
2020-04-13 19:21:48 -07:00
|
|
|
|
2020-08-24 17:18:08 -07:00
|
|
|
expectSuccess(res)
|
2020-04-13 19:21:48 -07:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Refresh', async () => {
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
nock(MojangRestAPI.AUTH_ENDPOINT)
|
2020-04-13 19:21:48 -07:00
|
|
|
.post('/refresh')
|
|
|
|
.reply(200, (uri, requestBody: any): Session => {
|
|
|
|
const mockResponse: Session = {
|
|
|
|
accessToken: 'abc',
|
|
|
|
clientToken: requestBody.clientToken,
|
|
|
|
selectedProfile: {
|
|
|
|
id: 'def',
|
|
|
|
name: 'username'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(requestBody.requestUser) {
|
|
|
|
mockResponse.user = {
|
|
|
|
id: 'def',
|
|
|
|
properties: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mockResponse
|
|
|
|
})
|
|
|
|
|
2020-08-27 18:27:56 -07:00
|
|
|
const res = await MojangRestAPI.refresh('gfd', 'xxx', true)
|
2020-08-24 17:18:08 -07:00
|
|
|
expectSuccess(res)
|
2020-04-13 19:21:48 -07:00
|
|
|
expect(res.data!.clientToken).to.equal('xxx')
|
|
|
|
expect(res.data).to.have.property('user')
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|