PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/lib/gitlab/api_authentication/builder_spec.rb

https://gitlab.com/18dit020/gitlab
Ruby | 76 lines | 58 code | 17 blank | 1 comment | 0 complexity | b130125f4b7facbc46963de4af2401e1 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'fast_spec_helper'
  3. RSpec.describe Gitlab::APIAuthentication::Builder do
  4. describe '#build' do
  5. shared_examples 'builds the correct result' do |token_type:, sent_through:, builds:|
  6. context "with #{token_type.size} token type(s) and #{sent_through.size} sent through(s)" do
  7. it 'works when passed together' do
  8. strategies = described_class.new.build { |allow| allow.token_types(*token_type).sent_through(*sent_through) }
  9. expect(strategies).to eq(builds)
  10. end
  11. it 'works when token types are passed separately' do
  12. strategies = described_class.new.build { |allow| token_type.each { |t| allow.token_types(t).sent_through(*sent_through) } }
  13. expect(strategies).to eq(builds)
  14. end
  15. it 'works when sent throughs are passed separately' do
  16. strategies = described_class.new.build { |allow| sent_through.each { |s| allow.token_types(*token_type).sent_through(s) } }
  17. expect(strategies).to eq(builds)
  18. end
  19. it 'works when token types and sent throughs are passed separately' do
  20. strategies = described_class.new.build { |allow| token_type.each { |t| sent_through.each { |s| allow.token_types(t).sent_through(s) } } }
  21. expect(strategies).to eq(builds)
  22. end
  23. end
  24. end
  25. it_behaves_like 'builds the correct result',
  26. token_type: [:pat],
  27. sent_through: [:basic],
  28. builds: { basic: [:pat] }
  29. it_behaves_like 'builds the correct result',
  30. token_type: [:pat],
  31. sent_through: [:basic, :oauth],
  32. builds: { basic: [:pat], oauth: [:pat] }
  33. it_behaves_like 'builds the correct result',
  34. token_type: [:pat, :job],
  35. sent_through: [:basic],
  36. builds: { basic: [:pat, :job] }
  37. it_behaves_like 'builds the correct result',
  38. token_type: [:pat, :job],
  39. sent_through: [:basic, :oauth],
  40. builds: { basic: [:pat, :job], oauth: [:pat, :job] }
  41. context 'with a complex auth strategy' do
  42. it 'builds the correct result' do
  43. strategies = described_class.new.build do |allow|
  44. allow.token_types(:pat, :job, :deploy).sent_through(:http_basic, :oauth)
  45. allow.token_types(:pat).sent_through(:http_private, :query_private)
  46. allow.token_types(:oauth2).sent_through(:http_bearer, :query_access)
  47. end
  48. expect(strategies).to eq({
  49. http_basic: [:pat, :job, :deploy],
  50. oauth: [:pat, :job, :deploy],
  51. http_private: [:pat],
  52. query_private: [:pat],
  53. http_bearer: [:oauth2],
  54. query_access: [:oauth2]
  55. })
  56. end
  57. end
  58. end
  59. end