gitlab_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. # rubocop:disable RSpec/FilePath
  4. RSpec.describe GitLab, integration: true, required_envs: %w[GITLAB_ENDPOINT GITLAB_APITOKEN] do
  5. # rubocop:enable RSpec/FilePath
  6. let(:instance) { described_class.new(ENV['GITLAB_ENDPOINT'], ENV['GITLAB_APITOKEN']) }
  7. let(:issue_data) do
  8. {
  9. id: '1',
  10. title: 'Example issue',
  11. url: ENV['GITLAB_ISSUE_LINK'],
  12. icon_state: 'open',
  13. milestone: 'important milestone',
  14. assignees: ['zammad-robot'],
  15. labels: [
  16. {
  17. color: '#FF0000',
  18. text_color: '#FFFFFF',
  19. title: 'critical'
  20. },
  21. {
  22. color: '#0033CC',
  23. text_color: '#FFFFFF',
  24. title: 'label1'
  25. },
  26. {
  27. color: '#D1D100',
  28. text_color: '#1F1E24',
  29. title: 'special'
  30. }
  31. ],
  32. }
  33. end
  34. let(:invalid_issue_url) { "https://#{URI.parse(ENV['GITLAB_ISSUE_LINK']).host}/group/project/-/issues/1" }
  35. describe '#issues_by_urls' do
  36. let(:result) { instance.issues_by_urls([ issue_url ]) }
  37. context 'when issue exists' do
  38. let(:issue_url) { ENV['GITLAB_ISSUE_LINK'] }
  39. it 'returns a result list' do
  40. expect(result.size).to eq(1)
  41. end
  42. it 'returns issue data in the result list' do
  43. expect(result[0]).to eq(issue_data)
  44. end
  45. end
  46. context 'when issue does not exists' do
  47. let(:issue_url) { invalid_issue_url }
  48. it 'returns no result' do
  49. expect(result.size).to eq(0)
  50. end
  51. end
  52. end
  53. describe '#issue_by_url' do
  54. let(:result) { instance.issue_by_url(issue_url) }
  55. context 'when issue exists' do
  56. let(:issue_url) { ENV['GITLAB_ISSUE_LINK'] }
  57. it 'returns issue data' do
  58. expect(result).to eq(issue_data)
  59. end
  60. end
  61. context 'when issue does not exists' do
  62. let(:issue_url) { invalid_issue_url }
  63. it 'returns nil' do
  64. expect(result).to be_nil
  65. end
  66. end
  67. end
  68. describe '#variables' do
  69. describe 'Zammad ignores relative GitLab URLs #3830' do
  70. let(:endpoint) { ENV['GITLAB_ENDPOINT'].sub('api/graphql', 'subfolder/api/graphql') }
  71. let(:instance) { described_class.new(endpoint, ENV['GITLAB_APITOKEN']) }
  72. let(:issue_url) { "https://#{URI.parse(ENV['GITLAB_ISSUE_LINK']).host}/subfolder/group/project/-/issues/1" }
  73. let(:linked_issue) { GitLab::LinkedIssue.new(instance.client) }
  74. it 'does remove the subfolder from the fullpath to get the issue correctly' do
  75. expect(linked_issue.send(:variables, issue_url)[:fullpath]).to eq('group/project')
  76. end
  77. end
  78. end
  79. end