Skip to content

Commit

Permalink
Merge be641e1 into d238c5f
Browse files Browse the repository at this point in the history
  • Loading branch information
chargio committed Feb 6, 2018
2 parents d238c5f + be641e1 commit c53a2e4
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

.byebug_history
/.yardoc/
/coverage/

/doc/
16 changes: 5 additions & 11 deletions app/controllers/v1/spin_candidates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ def index

###
# Show (id: identification of the spin)
# Provides a view of the spin
# TODO: When authenticated, provide extended info
# users/<user_id>/spins/<spin_id_or_name> Get a specific spin of user
# Provides a view of the Spin Candidate
#
# @params id: integer id of the spin candidate
def show
if params[:user_id]
@user = User.find_by_github_login(params[:user_id])
return_response status: :not_found unless @user
@spin = SpinCandidate.find_by(user_id: @user.id, visible: true, id: params[:id]) || Spin.find_by(user_id: @user.id, visible: true, name: params[:id])
else
@spin = SpinCandidate.find_by(id: params[:id], visible: true) || Spin.find_by(name: params[:id], visible: true)
end
@spin = SpinCandidate.find_by(user_id: current_user.id, id: params[:id])
unless @spin
render_error_exchange(:spin_not_found, :not_found)
render_error_exchange(:spin_candidate_not_found, :not_found)
return
end
return_response @spin, :ok, {}
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/refresh_spin_candidates_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def perform(user:, token:)
full_name: repo.full_name,
user: user,
validation_log: "Pending validation")
if (spin_candidate.is_candidate? client: client)
if (spin_candidate.is_candidate? user: user)
spin_candidate.save
else
spin_candidate.destroy unless spin_candidate.new_record?
Expand Down
10 changes: 4 additions & 6 deletions app/models/providers/github_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class GithubManager < BaseManager
attr_reader :server_type, :github_access

def initialize(provider)
Rails.logger.info 'Generating new connection to Source Control'
Rails.logger.info 'Generating new connection to GitHub'

github_url = provider[:enterprise] ? URI::HTTPS.build(host: provider[:server],path: "api/#{provider[:version]}") : URI::HTTPS.build(host: provider[:server])
github_url = provider[:enterprise] ? URI::HTTPS.build(host: provider[:server],path: "api/#{provider[:version]}") : URI::HTTPS.build(host: provider[:server])
github_url = URI::HTTPS.build(host: provider[:server])

opts = {
Expand All @@ -17,7 +17,6 @@ def initialize(provider)
}

@github_access ||= Octokit::Client.new opts

@server_type = 'GitHub'
end

Expand Down Expand Up @@ -104,11 +103,10 @@ def candidate_spin?(full_name)
raise Octokit::NotFound if @github_access.nil?
begin
# GitHub returns false when there is no content, or the content otherwise
not (@github_access.contents(full_name, path: '/.manageiq-spin', accept: 'application/vnd.github.raw') == false)
rescue Octokit::NotFound
not (@github_access.contents(full_name, path: '/.manageiq-spin') == false)
rescue Octokit::NotFound, Octokit::InvalidRepository
return false
end
end

end
end
14 changes: 4 additions & 10 deletions app/models/spin_candidate.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
class SpinCandidate < ApplicationRecord
belongs_to :user
validates :full_name, :validation_log, presence: true

def is_candidate?(client:)
begin
raise Octokit::NotFound if client.github_access.nil?
if client.github_access.contents(full_name, path: '/.manageiq-spin')
true
end
rescue Octokit::NotFound
false
end
def is_candidate? user:
client = Providers::BaseManager.new(user.authentication_tokens.first.provider).get_connector
client.candidate_spin? full_name
end

end
4 changes: 2 additions & 2 deletions app/serializers/v1/application_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def attributes(*args)
end

def load_serialization_config_for(type = 'attributes')
@seed_data[@object.class.name.downcase.pluralize][type]
@seed_data[@object.class.name.tableize][type]
end

def serialization_columns
@serialization_columns ||= @seed_data[@object.class.name.downcase.pluralize]['columns']
@serialization_columns ||= @seed_data[@object.class.name.tableize]['columns']
end

def param_admin?
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/v1/spin_candidate_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module V1
class SpinCandidateSerializer < ApplicationSerializer
end
end
2 changes: 1 addition & 1 deletion app/serializers/v1/spin_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module V1
class SpinSerializer < ApplicationSerializer
class SpinSerializer < ApplicationSerializer
end
end
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ en:
user_not_found:
title: This user is not in the database
detail: Maybe the id of user is wrong
spin_candidate_not_found:
title: Spin Candidate not found in the database
detail: Check that the id of the spin exists
spin_candidate_not_owner:
title: You don't have access to the Spin Candidate
detail: The user is wrong or the Spin Candidate does not exist

12 changes: 10 additions & 2 deletions product/json_returns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ users:
github_company: "company"
spins:
attributes: ["id","user_login", "name", "description", "watchers_count" ,"stargazers_count"]
expand: ["readme","license_name","license_html_url","license_key","default_branch","user_id", "clone_url", "forks_count", "open_issues_count", "metadata","releases"]
staff: ["created_at","updated_at"]
expand: ["readme", "license_name", "license_html_url", "license_key", "default_branch", "user_id", "clone_url", "forks_count", "open_issues_count", "metadata", "releases"]
staff: ["created_at", "updated_at"]
admin: ["visible", "published"]
columns:
id: "id"

spin_candidates:
attributes: ["id", "full_name", "validation_log"]
expand: []
staff: ["created_at", "updated_at"]
admin: []
columns:
id: "id"

8 changes: 7 additions & 1 deletion spec/factories/spin_candidates.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
FactoryBot.define do
factory :spin_candidate do

user
full_name { "#{user.github_login}/#{Faker::App.name.parameterize}" }
validation_log 'Pending validation'

trait :validation_log_full do
validation_log { Faker::Lorem.sentence }
end
end
end
66 changes: 54 additions & 12 deletions spec/models/spin_candidate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
require 'rails_helper'

RSpec.describe SpinCandidate, type: :model do
context "when not authenticated " do
pending "index"
pending "show"
pending "refresh"
let(:spin_candidate) { FactoryBot.build(:spin_candidate) }
let!(:user) { FactoryBot.create(:user) }
let(:non_valid_repo) { 'ManageIQ-Exchange/manageiq-exchange' }
let(:nonexisting_repo) { 'ManageIQ-Exchange/i_do-not_exist' }
let(:valid_repo) { 'ManageIQ-Exchange/manageiq-exchange-spin-template' }
it 'has a valid factory' do
spin_candidate.valid?
expect(spin_candidate).to be_valid
end
context "when authenticated" do
pending "index"
pending "show when valid id"
pending "show when invalid id"
pending "refresh when same repos"
pending "refresh when deleted repos"
pending "refresh when new repos"
pending "refresh when updated repos"

it 'is not valid without a user' do
spin_candidate.user = nil
spin_candidate.valid?
expect(spin_candidate.errors.details[:user]).to include(error: :blank)
end

it 'is not valid without a full name' do
spin_candidate.full_name = nil
spin_candidate.valid?
expect(spin_candidate.errors.details[:full_name]).to include(error: :blank)
end

it 'is not valid without a validation_log' do
spin_candidate.validation_log = nil
spin_candidate.valid?
expect(spin_candidate.errors.details[:validation_log]).to include(error: :blank)
end

it 'verifies a non-valid repo' do
spin_candidate.full_name = non_valid_repo
@user = user
api_basic_authorize
VCR.use_cassette('providers/github/spin_candidates/get-non-valid-repo') do
expect(spin_candidate.is_candidate?(user: user)).to be_falsy
end

end

it 'verifies a non-existing repo' do
spin_candidate.full_name = nonexisting_repo
@user = user
api_basic_authorize
VCR.use_cassette('providers/github/spin_candidates/get-non-existing-repo') do
expect(spin_candidate.is_candidate?(user: user)).to be_falsy
end

end

it 'verifies a valid repo' do
spin_candidate.full_name = valid_repo
@user = user
api_basic_authorize
VCR.use_cassette('providers/github/spin_candidates/get-valid-repo') do
expect(spin_candidate.is_candidate?(user: user)).to be_truthy
end
end
end
63 changes: 63 additions & 0 deletions spec/requests/v1/spin_candidate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rails_helper'

RSpec.describe 'V1::SpinCandidates', type: :request do
let!(:prefix) { 'v1' }

context 'when not authenticated' do
it '#index returns 401' do
get "/#{prefix}/spin_candidates"
expect(response).to have_http_status(401)
end

it '#show returns 401' do
get "/#{prefix}/spin_candidates/1"
expect(response).to have_http_status(401)
end

it '#publish returns 401' do
post "/#{prefix}/spin_candidates/1/publish"
expect(response).to have_http_status(401)
end

it '#refresh returns 401' do
post "/#{prefix}/spin_candidates/refresh"
expect(response).to have_http_status(401)
end
end
context 'when authenticated' do
let!(:user) { FactoryBot.create(:user) }
let!(:spin_candidate) { FactoryBot.create(:spin_candidate, user: user) }
let!(:spin_candidate1) { FactoryBot.create(:spin_candidate, user: user) }

it '#index' do
@user = user
api_basic_authorize
get "/#{prefix}/spin_candidates"
expect(response).to have_http_status(:ok)
data = JSON.parse(response.body)["data"]
expect(data).to include ({"id" => spin_candidate.id, "full_name" => spin_candidate.full_name, "validation_log" => spin_candidate.validation_log})
expect(data).to include ({"id" => spin_candidate1.id, "full_name" => spin_candidate1.full_name, "validation_log" => spin_candidate1.validation_log})
end


it "show when valid id" do
@user = user
api_basic_authorize
get "/#{prefix}/spin_candidates/#{spin_candidate.id}"
expect(response).to have_http_status(:ok)
data = JSON.parse(response.body)["data"]
expect(data).to include ({"id" => spin_candidate.id, "full_name" => spin_candidate.full_name, "validation_log" => spin_candidate.validation_log})
end

it 'show when invalid id' do
@user = user
api_basic_authorize
get "/#{prefix}/spin_candidates/error"
expect(response).to have_http_status(:not_found)
end
pending "refresh when same repos"
pending "refresh when deleted repos"
pending "refresh when new repos"
pending "refresh when updated repos"
end
end
65 changes: 65 additions & 0 deletions spec/vcr_cassettes/providers/github/spins_candidates/get-index.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c53a2e4

Please sign in to comment.