Commit 88af5b85 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Refactored GeoNode specs and added "missing_oauth_application?"

parent 1607581e
......@@ -56,6 +56,10 @@ class GeoNode < ActiveRecord::Base
URI.join(uri, "#{uri.path}/", "api/#{API::API.version}/geo/refresh_projects").to_s
end
def missing_oauth_application?
self.primary? ? false : !oauth_application.present?
end
private
def refresh_bulk_notify_worker_status
......
require 'spec_helper'
describe GeoNode, type: :model do
subject(:new_node) { described_class.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
subject(:new_primary_node) { described_class.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab', primary: true) }
subject(:empty_node) { described_class.new(schema: nil, host: nil, port: nil, relative_url_root: nil) }
let(:dummy_url) { 'https://localhost:3000/gitlab' }
context 'associations' do
......@@ -11,6 +15,7 @@ describe GeoNode, type: :model do
context 'default values' do
let(:gitlab_host) { 'gitlabhost' }
before(:each) { allow(Gitlab.config.gitlab).to receive(:host) { gitlab_host } }
subject { described_class.new }
it 'defines a default schema' do
expect(subject.schema).to eq('http')
......@@ -47,83 +52,76 @@ describe GeoNode, type: :model do
context 'dependent models for GeoNode' do
let(:geo_node_key_attributes) { FactoryGirl.build(:geo_node_key).attributes }
subject { GeoNode.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
context 'on initialize' do
before(:each) do
subject.geo_node_key_attributes = geo_node_key_attributes
new_node.geo_node_key_attributes = geo_node_key_attributes
end
it 'initializes a corresponding key' do
expect(subject.geo_node_key).to be_present
expect(new_node.geo_node_key).to be_present
end
it 'initializes a corresponding oauth application' do
expect(subject.oauth_application).to be_present
expect(new_node.oauth_application).to be_present
end
it 'is valid' do
expect(subject).to be_valid
expect(new_node).to be_valid
end
end
context 'on create' do
before(:each) do
subject.geo_node_key_attributes = geo_node_key_attributes
new_node.geo_node_key_attributes = geo_node_key_attributes
new_primary_node.geo_node_key_attributes = geo_node_key_attributes
end
it 'saves a corresponding key' do
subject.save!
expect(subject.geo_node_key).to be_persisted
new_node.save!
expect(new_node.geo_node_key).to be_persisted
end
it 'saves a corresponding oauth application if it is a secondary node' do
subject.save!
expect(subject.oauth_application).to be_persisted
new_node.save!
expect(new_node.oauth_application).to be_persisted
end
it 'has no oauth_application if it is a primary node' do
subject.primary=true
subject.save!
new_primary_node.save!
expect(subject.oauth_application).not_to be_present
expect(new_primary_node.oauth_application).not_to be_present
end
end
end
describe '#uri' do
context 'when all fields are filled' do
subject { GeoNode.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
it 'returns an URI object' do
expect(subject.uri).to be_a URI
expect(new_node.uri).to be_a URI
end
it 'includes schema home port and relative_url' do
expected_uri = URI.parse(dummy_url)
expect(subject.uri).to eq(expected_uri)
expect(new_node.uri).to eq(expected_uri)
end
end
context 'when required fields are not filled' do
subject { GeoNode.new(schema: nil, host: nil, port: nil, relative_url_root: nil) }
it 'returns an URI object' do
expect(subject.uri).to be_a URI
expect(empty_node.uri).to be_a URI
end
end
end
describe '#url' do
subject { GeoNode.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
it 'returns a string' do
expect(subject.url).to be_a String
expect(new_node.url).to be_a String
end
it 'includes schema home port and relative_url' do
expected_url = 'https://localhost:3000/gitlab'
expect(subject.url).to eq(expected_url)
expect(new_node.url).to eq(expected_url)
end
end
......@@ -161,11 +159,27 @@ describe GeoNode, type: :model do
end
describe '#notify_url' do
subject { GeoNode.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
let(:refresh_url) { 'https://localhost:3000/gitlab/api/v3/geo/refresh_projects' }
it 'returns api url based on node uri' do
expect(subject.notify_url).to eq(refresh_url)
expect(new_node.notify_url).to eq(refresh_url)
end
end
describe '#missing_oauth_application?' do
context 'on a primary node' do
it 'returns false' do
expect(new_primary_node).not_to be_missing_oauth_application
end
end
it 'returns false when present' do
expect(new_node).not_to be_missing_oauth_application
end
it 'returns true when it is not present' do
new_node.oauth_application = nil
expect(new_node).to be_missing_oauth_application
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment