require 'spec_helper' describe User, "valid" do subject { Factory(:user) } it { should validate_presence_of(:name) } it { should have_many(:account_memberships) } it { should have_many(:accounts).through(:account_memberships) } it { should have_many(:projects) } it { should have_many(:project_memberships) } it "is an admin of an account with an admin membership" do account = Factory(:account) Factory(:account_membership, :user => subject, :admin => true, :account => account) subject.should be_admin_of(account) end it "isn't an admin of an account with a non admin membership" do account = Factory(:account) Factory(:account_membership, :user => subject, :admin => false, :account => account) subject.should_not be_admin_of(account) end it "isn't an admin of an account without a membership" do account = Factory(:account) subject.should_not be_admin_of(account) end it "is a member with a membership for the given account" do account = Factory(:account) Factory(:account_membership, :user => subject, :account => account) subject.should be_member_of(account) end it "isn't a member without a membership for the given account" do account = Factory(:account) other_account = Factory(:account) Factory(:account_membership, :user => subject, :account => other_account) subject.should_not be_member_of(account) end it "is a member with a membership for the given project" do project = Factory(:project) Factory(:account_membership, :user => subject, :account => project.account) Factory(:project_membership, :user => subject, :project => project) subject.should be_member_of(project) end it "isn't a member without a membership for the given project" do project = Factory(:project) other_project = Factory(:project) Factory(:account_membership, :user => subject, :account => other_project.account) Factory(:project_membership, :user => subject, :project => other_project) subject.should_not be_member_of(project) end it "returns users by name" do Factory(:user, :name => "def") Factory(:user, :name => "abc") Factory(:user, :name => "ghi") User.by_name.map(&:name).should == %w(abc def ghi) end context "updating the permissions for an account" do let!(:account) { Factory(:account) } let!(:other_account) { Factory(:account) } let!(:member_project) { Factory(:project, :account => account) } let!(:nonmember_project) { Factory(:project, :account => account) } let!(:other_account_project) { Factory(:project, :account => other_account) } before do Factory(:account_membership, :account => account, :user => subject) Factory(:account_membership, :account => other_account, :user => subject) Factory(:project_membership, :project => member_project, :user => subject) Factory(:project_membership, :project => other_account_project, :user => subject) subject.reload subject.update_permissions_for(account, [nonmember_project.id]) end it "adds an added project for that account" do subject.should be_member_of(nonmember_project) end it "removes a removed project for that account" do subject.should_not be_member_of(member_project) end it "ignores a project for another account" do subject.should be_member_of(other_account_project) end end end