From a64f174d83caddfd3fe31698b5b2fd7701b5a573 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Mon, 29 Aug 2016 14:41:39 -0500 Subject: Refactored JSON header generation to its own class and added tests for it --- lib/gitlab_lfs_authentication.rb | 22 ++++++++++++++++++++++ lib/gitlab_shell.rb | 12 ++---------- spec/gitlab_lfs_authentication_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 lib/gitlab_lfs_authentication.rb create mode 100644 spec/gitlab_lfs_authentication_spec.rb diff --git a/lib/gitlab_lfs_authentication.rb b/lib/gitlab_lfs_authentication.rb new file mode 100644 index 0000000..b05da21 --- /dev/null +++ b/lib/gitlab_lfs_authentication.rb @@ -0,0 +1,22 @@ +require 'base64' +require 'json' + +class GitlabLfsAuthentication + attr_accessor :user, :repository_http_path + + def initialize(user, repository_http_path) + @user = user + @repository_http_path = repository_http_path + end + + def authenticate! + authorization = { + header: { + Authorization: "Basic #{Base64.strict_encode64("#{user['username']}:#{user['lfs_token']}")}" + }, + href: "#{repository_http_path}/info/lfs/" + } + + JSON.generate(authorization) + end +end diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb index ab98a6e..87fa347 100644 --- a/lib/gitlab_shell.rb +++ b/lib/gitlab_shell.rb @@ -1,8 +1,7 @@ require 'shellwords' -require 'base64' -require 'json' require_relative 'gitlab_net' +require_relative 'gitlab_lfs_authentication' class GitlabShell class AccessDeniedError < StandardError; end @@ -195,14 +194,7 @@ class GitlabShell def lfs_authenticate return unless user - authorization = { - header: { - Authorization: "Basic #{Base64.strict_encode64("#{user['username']}:#{user['lfs_token']}")}" - }, - href: "#{repository_http_path}/info/lfs/" - } - - puts JSON.generate(authorization) + puts GitlabLfsAuthentication.new(user, repository_http_path).authenticate! end private diff --git a/spec/gitlab_lfs_authentication_spec.rb b/spec/gitlab_lfs_authentication_spec.rb new file mode 100644 index 0000000..f4fee77 --- /dev/null +++ b/spec/gitlab_lfs_authentication_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' +require 'gitlab_lfs_authentication' + +describe GitlabLfsAuthentication do + let(:user) { { 'username' => 'dzaporozhets', 'lfs_token' => 'wsnys8Zm8Jn7zyhHTAAK' } } + + subject do + GitlabLfsAuthentication.new(user, 'http://gitlab.dev/repo') + end + + describe '#initialize' do + it { subject.user.should == user } + it { subject.repository_http_path.should == 'http://gitlab.dev/repo' } + end + + describe '#authenticate!' do + result = "{\"header\":{\"Authorization\":\"Basic ZHphcG9yb3poZXRzOndzbnlzOFptOEpuN3p5aEhUQUFL\"},\"href\":\"http://gitlab.dev/repo/info/lfs/\"}" + + it { subject.authenticate!.should == result } + end +end -- cgit v1.2.1