Skip to content

Commit

Permalink
GitService: Raises error about missing credentials only on instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
neomilium committed Oct 6, 2021
1 parent ce795d5 commit bb02241
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion features/update/pull_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Feature: Create a pull-request/merge-request after update
<%= @configs['name'] %>
"""
When I run `msync update --noop --pr`
Then the stderr should contain "A token is require to use services from gitlab"
Then the stderr should contain "A token is required to use services from gitlab"
And the exit status should be 1
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"

Expand Down
12 changes: 5 additions & 7 deletions lib/modulesync/git_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ module GitService
class MissingCredentialsError < Error; end

def self.instantiate(type:, endpoint:, token:)
raise MissingCredentialsError, <<~MESSAGE if token.nil?
A token is required to use services from #{type}:
Please set environment variable: "#{type.upcase}_TOKEN" or set the token entry in module options.
MESSAGE

case type
when :github
raise ModuleSync::Error, 'No GitHub token specified to create a pull request' if token.nil?
require 'modulesync/git_service/github'
ModuleSync::GitService::GitHub.new(token, endpoint)
when :gitlab
raise ModuleSync::Error, 'No GitLab token specified to create a merge request' if token.nil?
require 'modulesync/git_service/gitlab'
ModuleSync::GitService::GitLab.new(token, endpoint)
else
Expand Down Expand Up @@ -113,11 +116,6 @@ def self.token_for(sourcecode:, type:)
ENV['GITLAB_TOKEN']
end

raise MissingCredentialsError, <<~MESSAGE if token.nil?
A token is require to use services from #{type}:
Please set environment variable: "#{type.upcase}_TOKEN" or set the token entry in module options.
MESSAGE

token
end

Expand Down
22 changes: 14 additions & 8 deletions spec/unit/modulesync/git_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

context 'when instantiate a GitHub service without credentials' do
it 'raises an error' do
expect { ModuleSync::GitService.instantiate(type: :github, endpoint: nil, token: nil) }.to raise_error(ModuleSync::Error, 'No GitHub token specified to create a pull request')
expect { ModuleSync::GitService.instantiate(type: :github, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError)
end
end

context 'when instantiate a GitLab service without credentials' do
it 'raises an error' do
expect { ModuleSync::GitService.instantiate(type: :gitlab, endpoint: nil, token: nil) }.to raise_error(ModuleSync::Error, 'No GitLab token specified to create a merge request')
expect { ModuleSync::GitService.instantiate(type: :gitlab, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError)
end
end

Expand Down Expand Up @@ -90,9 +90,12 @@
end

context 'without any environment variable sets' do
it 'raises an error about missing credential' do
expect{ModuleSync::GitService.configuration_for(sourcecode: sourcecode)}
.to raise_error ModuleSync::GitService::MissingCredentialsError
it 'returns a nil token' do
expect(ModuleSync::GitService.configuration_for(sourcecode: sourcecode)).to eq({
type: :gitlab,
endpoint: 'https://git.example.com/api/v4',
token: nil,
})
end
end
end
Expand Down Expand Up @@ -120,9 +123,12 @@
end

context 'without a GITLAB_TOKEN environment variable sets' do
it 'raise an error about missing credential' do
expect{ModuleSync::GitService.configuration_for(sourcecode: sourcecode)}
.to raise_error ModuleSync::Error
it 'returns a nil token' do
expect(ModuleSync::GitService.configuration_for(sourcecode: sourcecode)).to eq({
type: :gitlab,
endpoint: 'https://gitlab.example.com/api/v4',
token: nil,
})
end
end
end
Expand Down

0 comments on commit bb02241

Please sign in to comment.