authentication-test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const nock = require('nock')
  2. const GitHub = require('../../')
  3. require('../mocha-node-setup')
  4. describe('authentication', () => {
  5. let github
  6. beforeEach(() => {
  7. github = new GitHub({
  8. baseUrl: 'https://authentication-test-host.com'
  9. })
  10. })
  11. it('basic', () => {
  12. nock('https://authentication-test-host.com', {
  13. reqheaders: {
  14. authorization: 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
  15. }
  16. })
  17. .get('/orgs/myorg')
  18. .reply(200, {})
  19. github.authenticate({
  20. type: 'basic',
  21. username: 'username',
  22. password: 'password'
  23. })
  24. return github.orgs.get({org: 'myorg'})
  25. })
  26. it('token', () => {
  27. nock('https://authentication-test-host.com', {
  28. reqheaders: {
  29. authorization: 'token abc4567'
  30. }
  31. })
  32. .get('/orgs/myorg')
  33. .reply(200, {})
  34. github.authenticate({
  35. type: 'token',
  36. token: 'abc4567'
  37. })
  38. return github.orgs.get({org: 'myorg'})
  39. })
  40. it('oauth token', () => {
  41. nock('https://authentication-test-host.com')
  42. .get('/orgs/myorg')
  43. .query({access_token: 'abc4567'})
  44. .reply(200, {})
  45. github.authenticate({
  46. type: 'oauth',
  47. token: 'abc4567'
  48. })
  49. return github.orgs.get({org: 'myorg'})
  50. })
  51. it('oauth token with query', () => {
  52. nock('https://authentication-test-host.com')
  53. .get('/orgs/myorg/repos')
  54. .query({per_page: 1, access_token: 'abc4567'})
  55. .reply(200, [])
  56. github.authenticate({
  57. type: 'oauth',
  58. token: 'abc4567'
  59. })
  60. return github.repos.getForOrg({org: 'myorg', per_page: 1})
  61. })
  62. it('oauth key & secret', () => {
  63. nock('https://authentication-test-host.com')
  64. .get('/orgs/myorg')
  65. .query({client_id: 'oauthkey', client_secret: 'oauthsecret'})
  66. .reply(200, {})
  67. github.authenticate({
  68. type: 'oauth',
  69. key: 'oauthkey',
  70. secret: 'oauthsecret'
  71. })
  72. return github.orgs.get({org: 'myorg'})
  73. })
  74. it('oauth key & secret with query', () => {
  75. nock('https://authentication-test-host.com')
  76. .get('/orgs/myorg/repos')
  77. .query({per_page: 1, client_id: 'oauthkey', client_secret: 'oauthsecret'})
  78. .reply(200, [])
  79. github.authenticate({
  80. type: 'oauth',
  81. key: 'oauthkey',
  82. secret: 'oauthsecret'
  83. })
  84. return github.repos.getForOrg({org: 'myorg', per_page: 1})
  85. })
  86. it('app', () => {
  87. nock('https://authentication-test-host.com', {
  88. reqheaders: {
  89. authorization: 'Bearer abc4567'
  90. }
  91. })
  92. .get('/orgs/myorg')
  93. .reply(200, {})
  94. github.authenticate({
  95. type: 'app',
  96. token: 'abc4567'
  97. })
  98. return github.orgs.get({org: 'myorg'})
  99. })
  100. it('authenticate without options', () => {
  101. github.authenticate()
  102. })
  103. it('authenticate errors', () => {
  104. expect(() => {
  105. github.authenticate({})
  106. }).to.throw(Error)
  107. expect(() => {
  108. github.authenticate({type: 'basic'})
  109. }).to.throw(Error)
  110. expect(() => {
  111. github.authenticate({type: 'oauth'})
  112. }).to.throw(Error)
  113. expect(() => {
  114. github.authenticate({type: 'token'})
  115. }).to.throw(Error)
  116. })
  117. })