require 'spec_helper' describe Project do it { should belong_to(:account) } it { should validate_presence_of(:account_id) } it { should have_many(:permissions) } it { should have_many(:users).through(:permissions) } it { should validate_presence_of(:keyword) } it { should validate_presence_of(:name) } it "finds projects visible to a user" do account = Factory(:account) user = Factory(:user) membership = Factory(:membership, :user => user, :account => account) visible_projects = [Factory(:project, :account => account), Factory(:project, :account => account)] invisible_project = Factory(:project, :account => account) visible_projects.each do |visible_project| Factory(:permission, :project => visible_project, :membership => membership) end Project.visible_to(user).to_a.should =~ visible_projects end it "returns projects by name" do Factory(:project, :name => "def") Factory(:project, :name => "abc") Factory(:project, :name => "ghi") Project.by_name.map(&:name).should == %w(abc def ghi) end it "should give its keyword for to_param" do project = Factory(:project) project.to_param.should == project.keyword end end describe Project, "keyword uniqueness" do let(:project) { Factory(:project) } subject do Factory.build(:project, :account => project.account) end it "validates uniqueness of it's keyword" do subject.keyword = project.keyword subject.save subject.errors[:keyword].should include("has already been taken") end end describe Project, "for an account with admin and non-admin users" do let!(:account) { Factory(:account, :name => "Account") } let!(:other_account) { Factory(:account, :name => "Other") } let!(:non_admin) { Factory(:user) } let!(:admins) { [Factory(:user), Factory(:user)] } let!(:non_member) { Factory(:user) } subject { Factory(:project, :account => account) } before do Factory(:membership, :account => account, :user => non_admin, :admin => false) Factory(:membership, :account => other_account, :user => non_member, :admin => true) admins.each do |admin| Factory(:membership, :user => admin, :account => account, :admin => true) end end it "has users" do subject.users.should_not be_empty end it "is viewable by admins by default" do admins.each do |admin| should have_member(admin) end end it "isn't viewable by non-members" do should_not have_member(non_admin) should_not have_member(non_member) end end describe Project, "saved" do subject { Factory(:project) } it "has a member with a membership" do user = Factory(:user) membership = Factory(:membership, :account => subject.account, :user => user) membership = Factory(:permission, :project => subject, :membership => membership) should have_member(user) end it "doesn't have a member without a membership" do user = Factory(:user) should_not have_member(user) end end describe Project, "assigning a new user list" do subject { Factory(:project) } let(:account) { subject.account } let!(:member) { Factory(:user) } let!(:non_member) { Factory(:user) } before do member_membership = Factory(:membership, :account => account, :user => member) non_member_membership = Factory(:membership, :account => account, :user => non_member) Factory(:permission, :membership => member_membership, :project => subject) subject.reload.update_attributes!(:user_ids => [non_member.id, ""]) end it "adds an added user" do member.should_not be_member_of(subject) end it "removes a removed user" do non_member.should be_member_of(subject) end end