| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | require 'db/mongo/models/user' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Devops::Model::User, type: :model do | 
					
						
							|  |  |  |   let(:user) { build(:user) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'is valid with correct attrs' do | 
					
						
							|  |  |  |     expect(user).to be_valid | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe 'validation' do | 
					
						
							|  |  |  |     include_examples 'field type validation', :id, :not_nil, :non_empty_string, :only_word_symbols, :field_validator | 
					
						
							|  |  |  |     include_examples 'field type validation', :password, :not_nil, :maybe_empty_string, :field_validator | 
					
						
							|  |  |  |     include_examples 'field type validation', :email, :not_nil, :maybe_empty_string, :field_validator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "privileges is set to default value if nil is passed" do | 
					
						
							|  |  |  |       expect(user.privileges).not_to be nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'privileges should be a hash' do | 
					
						
							|  |  |  |       expect(build(:user, privileges: 'root')).not_to be_valid | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it '#to_hash_without_id returns email, password and privileges' do | 
					
						
							|  |  |  |     expect(user.to_hash_without_id.keys).to match_array(%w(email password privileges)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#privileges' do | 
					
						
							|  |  |  |     subject {user.privileges} | 
					
						
							|  |  |  |     it { should be_a(Hash) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'has entity types as keys' do | 
					
						
							|  |  |  |       expect(subject.keys).to match_array(Devops::Model::User::KNOWN_ENTITIES) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#all_privileges' do | 
					
						
							|  |  |  |     it 'all privileges are set to rwx' do | 
					
						
							|  |  |  |       expect(user.all_privileges.values.uniq).to eq ['rwx'] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#default_privileges' do | 
					
						
							|  |  |  |     it 'user privilege is empty' do | 
					
						
							|  |  |  |       expect(user.default_privileges['user']).to be_empty | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'others are r' do | 
					
						
							|  |  |  |       result = user.default_privileges | 
					
						
							|  |  |  |       result.delete('user') | 
					
						
							|  |  |  |       expect(result.values.uniq).to eq ['r'] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#grant' do | 
					
						
							|  |  |  |     it 'sets privilege' do | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         user.grant('key', 'rw') | 
					
						
							|  |  |  |       }.to change{ user.privileges['key'] }.from('r').to('rw') | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     it 'could not be applied to root' do | 
					
						
							|  |  |  |       root = build(:user, id: 'root') | 
					
						
							|  |  |  |       expect{root.grant('user', 'r')}.to raise_error(InvalidPrivileges) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'recognize all value of entity' do | 
					
						
							|  |  |  |       user.grant('all', 'rw') | 
					
						
							|  |  |  |       expect(user.privileges.values.uniq).to eq ['rw'] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'sets default privileges if cmd is empty' do | 
					
						
							|  |  |  |       user.grant('user', 'rw') | 
					
						
							|  |  |  |       user.grant('', 'rwx') | 
					
						
							|  |  |  |       expect(user.privileges).to eq user.default_privileges | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'could not be applied with unknown privilege value' do | 
					
						
							|  |  |  |       expect{user.grant('user', 'q')}.to raise_error(InvalidCommand) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 14:49:30 +03:00
										 |  |  |   describe '.build_from_bson' do | 
					
						
							|  |  |  |     it 'builds User model from given hash and assigns id' do | 
					
						
							|  |  |  |       model = described_class.build_from_bson('_id' => 'foo', 'username' => 'not shown', 'email' => 'baz') | 
					
						
							|  |  |  |       expect(model.id).to eq 'foo' | 
					
						
							|  |  |  |       expect(model.email).to eq 'baz' | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |   describe '#check_privileges' do | 
					
						
							|  |  |  |     it "raises InvalidPrivileges if user hasn't specified privilege" do | 
					
						
							|  |  |  |       expect { user.check_privileges('key', 'w') }.to raise_error(InvalidPrivileges) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'does nothing is user has specified privilege' do | 
					
						
							| 
									
										
										
										
											2016-01-20 14:49:30 +03:00
										 |  |  |       expect{user.check_privileges('key', 'r')}.not_to raise_error | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'raises InvalidPrivileges if given privelege is wrong' do | 
					
						
							|  |  |  |       expect{user.check_privileges('key', 't')}.to raise_error InvalidPrivileges | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '.create_root' do | 
					
						
							|  |  |  |     subject {Devops::Model::User.create_root} | 
					
						
							|  |  |  |     it { should be_valid } | 
					
						
							|  |  |  |     it 'his name is root' do | 
					
						
							|  |  |  |       expect(subject.id).to eq Devops::Model::User::ROOT_USER_NAME | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'his password is root password' do | 
					
						
							|  |  |  |       expect(subject.password).to eq Devops::Model::User::ROOT_PASSWORD | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'has all privileges' do | 
					
						
							|  |  |  |       expect(subject.privileges).to eq user.all_privileges | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |