mojangTest.ts 5.1 KB

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