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