| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  | require 'db/mongo/models/project' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Devops::Model::Project, type: :model do | 
					
						
							|  |  |  |   let(:project) { build(:project) } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   describe 'validation rules:', stubbed_logger: true do | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  |     include_examples 'field type validation', :description, :maybe_nil, :maybe_empty_string | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     include_examples 'field type validation', :named_tasks, :not_nil, :maybe_empty_array | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe 'named_tasks validation' do | 
					
						
							|  |  |  |       it 'is valid with array of hashes like {"name" => "foo", "run_list" => ["role[bar]"]}' do | 
					
						
							|  |  |  |         expect(build(:project, named_tasks: [{"name" => "foo", "run_list" => ["role[bar]"]}] )).to be_valid | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "isn't valid with array of hashes with invalid run_list elements" do | 
					
						
							|  |  |  |         expect(build(:project, named_tasks: [{"name" => "foo", "run_list" => ["bar"]}] )).not_to be_valid | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "isn't valid with array of hashes without name or run_list" do | 
					
						
							|  |  |  |         expect(build(:project, named_tasks: [{run_list: ["role[bar]"]}] )).not_to be_valid | 
					
						
							|  |  |  |         expect(build(:project, named_tasks: [{"name" => "foo"}] )).not_to be_valid | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '.fields' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     it "should match array ['deploy_envs', 'type', 'description', 'named_tasks']" do | 
					
						
							|  |  |  |       expect(described_class.list_fields).to eq %w(deploy_envs type description named_tasks) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#initialize' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     it "doesn't set @archived to false by default" do | 
					
						
							|  |  |  |       expect(build(:project, archived: nil).archived).to eq nil | 
					
						
							| 
									
										
										
										
											2015-12-02 12:51:23 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   describe '#environment' do | 
					
						
							|  |  |  |     let(:project) { build(:project, with_env_identifiers: %w(foo bar)) } | 
					
						
							| 
									
										
										
										
											2015-12-02 12:51:23 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it 'returns found env' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(project.environment('bar')).to be_an(Devops::Model::Environment) | 
					
						
							| 
									
										
										
										
											2015-12-02 12:51:23 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'raises RecordNotFound if there is no such env' do | 
					
						
							|  |  |  |       expect { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         project.environment('missing') | 
					
						
							|  |  |  |       }.to raise_error Devops::Exception::RecordNotFound | 
					
						
							| 
									
										
										
										
											2015-12-02 12:51:23 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   describe '#add_environment', stubbed_logger: true do | 
					
						
							|  |  |  |     let(:env) {build(:environment)} | 
					
						
							|  |  |  |     subject { project.add_environment(env) } | 
					
						
							| 
									
										
										
										
											2015-12-02 12:51:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     it 'inserts deploy env' do | 
					
						
							|  |  |  |       expect(project).to receive(:push).with(hash_including(environments: env.to_hash)) | 
					
						
							| 
									
										
										
										
											2015-12-02 12:51:23 +03:00
										 |  |  |       subject | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |   describe '#add_authorized_user' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     let(:env1) { build(:environment, id: 'foo') } | 
					
						
							|  |  |  |     let(:env2) { build(:environment, id: 'bar') } | 
					
						
							|  |  |  |     let(:project) { build(:project, set_environments: [env1, env2]) } | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it 'returns nil if user is nil' do | 
					
						
							|  |  |  |       expect(project.add_authorized_user(nil)).to be_nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when env isn't given" do | 
					
						
							|  |  |  |       context 'when user is a String' do | 
					
						
							|  |  |  |         it 'adds given user to all envs' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#add_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.add_authorized_user('John') | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root John)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root John)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context 'when user is an Array of strings' do | 
					
						
							|  |  |  |         it 'adds given users to all envs' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#add_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.add_authorized_user(['John', 'Matz']) | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root John Matz)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root John Matz)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when env is given' do | 
					
						
							|  |  |  |       context 'when user is a String' do | 
					
						
							|  |  |  |         it 'adds given user to given env' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#add_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.add_authorized_user('John', 'bar') | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root John)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context 'when user is an Array of strings' do | 
					
						
							|  |  |  |         it 'adds given users to all envs' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#add_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.add_authorized_user(['John', 'Matz'], 'bar') | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root John Matz)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#remove_authorized_user' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     let(:env1) { build(:environment, id: 'foo', users: %w(root John Matz)) } | 
					
						
							|  |  |  |     let(:env2) { build(:environment, id: 'bar', users: %w(root John Matz)) } | 
					
						
							|  |  |  |     let(:project) { build(:project, environments: [env1, env2]) } | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it 'returns nil if user is nil' do | 
					
						
							|  |  |  |       expect(project.remove_authorized_user(nil)).to be_nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when env isn't given" do | 
					
						
							|  |  |  |       context 'when user is a String' do | 
					
						
							|  |  |  |         it 'adds given user to all envs' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#remove_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.remove_authorized_user('John') | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root Matz)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root Matz)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context 'when user is an Array of strings' do | 
					
						
							|  |  |  |         it 'adds given users to all envs' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#remove_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.remove_authorized_user(['John', 'Matz']) | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when env is given' do | 
					
						
							|  |  |  |       context 'when user is a String' do | 
					
						
							|  |  |  |         it 'adds given user to given env' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#remove_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.remove_authorized_user('John', 'bar') | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root John Matz)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root Matz)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context 'when user is an Array of strings' do | 
					
						
							|  |  |  |         it 'adds given users to all envs' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           pending 'bug: Devops::Db.connector in Project#remove_authorized_user' | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |           project.remove_authorized_user(['John', 'Matz'], 'bar') | 
					
						
							|  |  |  |           expect(env1.users).to match_array(%w(root John Matz)) | 
					
						
							|  |  |  |           expect(env2.users).to match_array(%w(root)) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#check_authorization' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     let(:env) { build(:environment, id: 'foo', users: %w(root Matz)) } | 
					
						
							|  |  |  |     let(:project) { build(:project, environments: [env]) } | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it 'returns true for root user' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(project.check_authorization('root', 'foo')).to be true | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns false if env's users don't include given user" do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(project.check_authorization('John', 'foo')).to be false | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns true if env's users include given user" do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(project.check_authorization('Matz', 'foo')).to be true | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'returns false if there is no such env' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(project.check_authorization('Matz', 'wrong')).to be false | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   describe '#delete_environment', clean_db_after_example: true do | 
					
						
							|  |  |  |     it 'removes env' do | 
					
						
							|  |  |  |       project.save | 
					
						
							|  |  |  |       project.delete_environment('foo') | 
					
						
							|  |  |  |       expect(project.reload.environments).to match_array [] | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#to_hash' do | 
					
						
							|  |  |  |     subject { project.to_hash } | 
					
						
							|  |  |  |     it 'returns hash' do | 
					
						
							|  |  |  |      expect(subject).to be_a(Hash) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'contains deploy_envs converted to hashes' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(subject['environments']).to be_an_array_of(Hash) | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 22:05:25 +03:00
										 |  |  |     it 'also contains descriptions, run_list and named_tasks' do | 
					
						
							|  |  |  |       expect(subject).to include('description', 'run_list', 'named_tasks') | 
					
						
							| 
									
										
										
										
											2015-12-02 20:29:09 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'contains archived key if project is archived' do | 
					
						
							|  |  |  |       project.archived = true | 
					
						
							|  |  |  |       expect(subject).to include('archived') | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "doesn't contain archived if project isn't archived" do | 
					
						
							|  |  |  |       expect(subject).not_to include('archived') | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 13:04:35 +03:00
										 |  |  |   describe '#to_hash_list' do | 
					
						
							|  |  |  |     it 'returns hash' do | 
					
						
							|  |  |  |       expect(project.to_hash_list).to be_a(Hash) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#deploy_info' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     subject { project.deploy_info(project.environment('foo')) } | 
					
						
							| 
									
										
										
										
											2015-12-07 13:04:35 +03:00
										 |  |  |     it 'returns hash' do | 
					
						
							|  |  |  |       expect(subject).to be_a(Hash) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'includes use_json_file, project and project_info' do | 
					
						
							|  |  |  |       expect(subject).to include('use_json_file', 'project', 'project_info') | 
					
						
							|  |  |  |       expect(subject['project_info']).to be_a(Hash) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  | end |