require 'spec_helper' describe Project do it { should belong_to(:account) } it { should validate_presence_of(:account_id) } it { should have_many(:project_memberships) } it { should have_many(:users) } it "finds projects visible to a user" do account = Factory(:account) user = Factory(:user) Factory(:account_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(:project_membership, :project => visible_project, :user => user) 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 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(:account_membership, :account => account, :user => non_admin, :admin => false) Factory(:account_membership, :account => other_account, :user => non_member, :admin => true) admins.each do |admin| Factory(:account_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) Factory(:account_membership, :account => subject.account, :user => user) membership = Factory(:project_membership, :project => subject, :user => user) 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