mojangTest.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Mojang } from "../../src/main/mojang/mojang"
  2. import { expect } from 'chai'
  3. import nock from 'nock'
  4. import { URL } from 'url'
  5. import { Session } from "../../src/main/model/mojang/auth/Session"
  6. import { MojangResponseCode } from "../../src/main/mojang/type/Response"
  7. function expectMojangResponse(res: any, responseCode: MojangResponseCode, negate = false) {
  8. expect(res).to.not.be.an('error')
  9. expect(res).to.be.an('object')
  10. expect(res).to.have.property('responseCode')
  11. if(!negate) {
  12. expect(res.responseCode).to.equal(responseCode)
  13. } else {
  14. expect(res.responseCode).to.not.equal(responseCode)
  15. }
  16. }
  17. describe('Mojang Errors', () => {
  18. it('Status (Offline)', async () => {
  19. const defStatusHack = Mojang['statuses']
  20. const url = new URL(Mojang.STATUS_ENDPOINT)
  21. nock(url.origin)
  22. .get(url.pathname)
  23. .reply(500, 'Service temprarily offline.')
  24. const res = await Mojang.status();
  25. expectMojangResponse(res, MojangResponseCode.SUCCESS, true)
  26. expect(res.data).to.be.an('array')
  27. expect(res.data).to.deep.equal(defStatusHack)
  28. }).timeout(2500)
  29. it('Authenticate (Invalid Credentials)', async () => {
  30. nock(Mojang.AUTH_ENDPOINT)
  31. .post('/authenticate')
  32. .reply(403, (uri, requestBody: any): { error: string, errorMessage: string } => {
  33. return {
  34. error: 'ForbiddenOperationException',
  35. errorMessage: 'Invalid credentials. Invalid username or password.'
  36. }
  37. })
  38. const res = await Mojang.authenticate('user', 'pass', 'xxx', true)
  39. expectMojangResponse(res, MojangResponseCode.ERROR_INVALID_CREDENTIALS)
  40. expect(res.data).to.be.a('null')
  41. expect(res.error).to.not.be.a('null')
  42. })
  43. })
  44. describe('Mojang Status', () => {
  45. it('Status (Online)', async () => {
  46. const defStatusHack = Mojang['statuses']
  47. const url = new URL(Mojang.STATUS_ENDPOINT)
  48. nock(url.origin)
  49. .get(url.pathname)
  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. })