mojangTest.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import { MojangRestAPI } from 'common/mojang/rest/MojangRestAPI'
  3. import { expect } from 'chai'
  4. import nock from 'nock'
  5. import { Session } from 'common/mojang/rest/Auth'
  6. import { MojangErrorCode, MojangResponse } from 'common/mojang/rest/internal/MojangResponse'
  7. import { RestResponseStatus, RestResponse } from 'common/got/RestResponse'
  8. function assertResponse(res: RestResponse<unknown>) {
  9. expect(res).to.not.be.an('error')
  10. expect(res).to.be.an('object')
  11. }
  12. function expectSuccess(res: RestResponse<unknown>) {
  13. assertResponse(res)
  14. expect(res).to.have.property('responseStatus')
  15. expect(res.responseStatus).to.equal(RestResponseStatus.SUCCESS)
  16. }
  17. function expectFailure(res: RestResponse<unknown>) {
  18. expect(res.responseStatus).to.not.equal(RestResponseStatus.SUCCESS)
  19. }
  20. function expectMojangResponse(res: MojangResponse<unknown>, responseCode: MojangErrorCode, negate = false) {
  21. assertResponse(res)
  22. expect(res).to.have.property('mojangErrorCode')
  23. if(!negate) {
  24. expect(res.mojangErrorCode).to.equal(responseCode)
  25. } else {
  26. expect(res.mojangErrorCode).to.not.equal(responseCode)
  27. }
  28. }
  29. describe('Mojang Errors', () => {
  30. after(() => {
  31. nock.cleanAll()
  32. })
  33. it('Status (Offline)', async () => {
  34. const defStatusHack = MojangRestAPI['statuses']
  35. nock(MojangRestAPI.STATUS_ENDPOINT)
  36. .get('/check')
  37. .reply(500, 'Service temprarily offline.')
  38. const res = await MojangRestAPI.status()
  39. expectFailure(res)
  40. expect(res.data).to.be.an('array')
  41. expect(res.data).to.deep.equal(defStatusHack)
  42. }).timeout(2500)
  43. it('Authenticate (Invalid Credentials)', async () => {
  44. nock(MojangRestAPI.AUTH_ENDPOINT)
  45. .post('/authenticate')
  46. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  47. .reply(403, (uri, requestBody: unknown): { error: string, errorMessage: string } => {
  48. return {
  49. error: 'ForbiddenOperationException',
  50. errorMessage: 'Invalid credentials. Invalid username or password.'
  51. }
  52. })
  53. const res = await MojangRestAPI.authenticate('user', 'pass', 'xxx', true)
  54. expectMojangResponse(res, MojangErrorCode.ERROR_INVALID_CREDENTIALS)
  55. expect(res.data).to.be.a('null')
  56. expect(res.error).to.not.be.a('null')
  57. })
  58. })
  59. describe('Mojang Status', () => {
  60. it('Status (Online)', async () => {
  61. const defStatusHack = MojangRestAPI['statuses']
  62. nock(MojangRestAPI.STATUS_ENDPOINT)
  63. .get('/check')
  64. .reply(200, defStatusHack)
  65. const res = await MojangRestAPI.status()
  66. expectSuccess(res)
  67. expect(res.data).to.be.an('array')
  68. expect(res.data).to.deep.equal(defStatusHack)
  69. }).timeout(2500)
  70. })
  71. describe('Mojang Auth', () => {
  72. it('Authenticate', async () => {
  73. nock(MojangRestAPI.AUTH_ENDPOINT)
  74. .post('/authenticate')
  75. .reply(200, (uri, requestBody: any): Session => {
  76. const mockResponse: Session = {
  77. accessToken: 'abc',
  78. clientToken: requestBody.clientToken,
  79. selectedProfile: {
  80. id: 'def',
  81. name: 'username'
  82. }
  83. }
  84. if(requestBody.requestUser) {
  85. mockResponse.user = {
  86. id: 'def',
  87. properties: []
  88. }
  89. }
  90. return mockResponse
  91. })
  92. const res = await MojangRestAPI.authenticate('user', 'pass', 'xxx', true)
  93. expectSuccess(res)
  94. expect(res.data!.clientToken).to.equal('xxx')
  95. expect(res.data).to.have.property('user')
  96. })
  97. it('Validate', async () => {
  98. nock(MojangRestAPI.AUTH_ENDPOINT)
  99. .post('/validate')
  100. .times(2)
  101. .reply((uri, requestBody: any) => {
  102. return [
  103. requestBody.accessToken === 'abc' ? 204 : 403
  104. ]
  105. })
  106. const res = await MojangRestAPI.validate('abc', 'def')
  107. expectSuccess(res)
  108. expect(res.data).to.be.a('boolean')
  109. expect(res.data).to.equal(true)
  110. const res2 = await MojangRestAPI.validate('def', 'def')
  111. expectSuccess(res2)
  112. expect(res2.data).to.be.a('boolean')
  113. expect(res2.data).to.equal(false)
  114. })
  115. it('Invalidate', async () => {
  116. nock(MojangRestAPI.AUTH_ENDPOINT)
  117. .post('/invalidate')
  118. .reply(204)
  119. const res = await MojangRestAPI.invalidate('adc', 'def')
  120. expectSuccess(res)
  121. })
  122. it('Refresh', async () => {
  123. nock(MojangRestAPI.AUTH_ENDPOINT)
  124. .post('/refresh')
  125. .reply(200, (uri, requestBody: any): Session => {
  126. const mockResponse: Session = {
  127. accessToken: 'abc',
  128. clientToken: requestBody.clientToken,
  129. selectedProfile: {
  130. id: 'def',
  131. name: 'username'
  132. }
  133. }
  134. if(requestBody.requestUser) {
  135. mockResponse.user = {
  136. id: 'def',
  137. properties: []
  138. }
  139. }
  140. return mockResponse
  141. })
  142. const res = await MojangRestAPI.refresh('gfd', 'xxx', true)
  143. expectSuccess(res)
  144. expect(res.data!.clientToken).to.equal('xxx')
  145. expect(res.data).to.have.property('user')
  146. })
  147. })