Fix few bugs and tests after refactoring ownership logic

This commit is contained in:
Dmitriy Zaporozhets 2013-01-02 19:32:34 +02:00
parent 00a1f5bc2c
commit d431e43392
19 changed files with 58 additions and 61 deletions

View file

@ -9,7 +9,7 @@ class Admin::UsersController < AdminController
def show
@admin_user = User.find(params[:id])
@projects = if @admin_user.projects.empty?
@projects = if @admin_user.authorized_projects.empty?
Project
else
Project.without_user(@admin_user)
@ -98,7 +98,7 @@ class Admin::UsersController < AdminController
def destroy
@admin_user = User.find(params[:id])
if @admin_user.my_own_projects.count > 0
if @admin_user.personal_projects.count > 0
redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return
end
@admin_user.destroy

View file

@ -1,6 +1,6 @@
module NamespacesHelper
def namespaces_options(selected = :current_user, scope = :default)
groups = current_user.namespaces.select {|n| n.type == 'Group'}
groups = current_user.owned_groups.select {|n| n.type == 'Group'}
users = if scope == :all
Namespace.root

View file

@ -29,21 +29,10 @@ class Ability
rules << project_guest_rules
end
if project.namespace
# If user own project namespace
# (Ex. group owner or account owner)
if project.namespace.owner == user
rules << project_admin_rules
end
else
# For compatibility with global projects
# use projects.owner_id
if project.owner == user
rules << project_admin_rules
end
if project.owner == user
rules << project_admin_rules
end
rules.flatten
end

View file

@ -73,7 +73,7 @@ class Key < ActiveRecord::Base
if is_deploy_key
[project]
else
user.projects
user.authorized_projects
end
end

View file

@ -80,7 +80,7 @@ class Project < ActiveRecord::Base
# Scopes
scope :public_only, where(private_flag: false)
scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) }
scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) }
scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
scope :sorted_by_activity, ->() { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") }

View file

@ -187,4 +187,9 @@ class User < ActiveRecord::Base
(projects.namespace_id IS NULL AND projects.creator_id = :user_id)",
namespaces: namespaces.map(&:id), user_id: self.id)
end
# Team membership in personal projects
def tm_in_personal_projects
personal_projects.users_projects.where(user_id: self.id)
end
end

View file

@ -69,7 +69,7 @@ module Account
def projects_limit_percent
return 100 if projects_limit.zero?
(my_own_projects.count.to_f / projects_limit) * 100
(personal_projects.count.to_f / projects_limit) * 100
end
def recent_push project_id = nil

View file

@ -28,7 +28,10 @@
%span.monospace= project.path_with_namespace + ".git"
%td= project.users_projects.count
%td
= link_to project.chief.name, [:admin, project.chief]
- if project.owner
= link_to project.owner.name, [:admin, project.owner]
- else
(deleted)
%td= last_commit(project)
%td= link_to 'Edit', edit_admin_project_path(project), id: "edit_#{dom_id(project)}", class: "btn small"
%td.bgred= link_to 'Destroy', [:admin, project], confirm: "REMOVE #{project.name}? Are you sure?", method: :delete, class: "btn small danger"

View file

@ -106,8 +106,8 @@
%td= link_to group.name, admin_group_path(group)
- if @admin_user.projects.present?
%h5 Projects:
- if @admin_user.personal_projects.present?
%h5 Personal Projects:
%br
%table.zebra-striped
@ -118,7 +118,7 @@
%th
%th
- @admin_user.users_projects.each do |tm|
- @admin_user.tm_in_personal_projects.each do |tm|
- project = tm.project
%tr
%td= link_to project.name_with_namespace, admin_project_path(project)

View file

@ -64,7 +64,7 @@
%legend
Personal projects:
%small.right
%span= current_user.my_own_projects.count
%span= current_user.personal_projects.count
of
%span= current_user.projects_limit
.padded

View file

@ -9,7 +9,7 @@ module Gitlab
# Example Request:
# GET /projects
get do
@projects = paginate current_user.projects
@projects = paginate current_user.authorized_projects
present @projects, with: Entities::Project
end

View file

@ -37,7 +37,7 @@ FactoryGirl.define do
end
factory :namespace do
sequence(:name) { |n| "group#{n}" }
sequence(:name) { |n| "namespace#{n}" }
path { name.downcase.gsub(/\s/, '_') }
owner
end

View file

@ -4,7 +4,7 @@ describe Gitlab::API do
include ApiHelpers
let(:user) { create(:user) }
let!(:project) { create(:project, owner: user) }
let!(:project) { create(:project, namespace: user.namespace ) }
let!(:issue) { create(:issue, author: user, assignee: user, project: project) }
before { project.add_access(user, :read) }

View file

@ -4,7 +4,7 @@ describe Gitlab::API do
include ApiHelpers
let(:user) { create(:user ) }
let!(:project) { create(:project, owner: user) }
let!(:project) { create(:project, namespace: user.namespace ) }
let!(:merge_request) { create(:merge_request, author: user, assignee: user, project: project, title: "Test") }
before { project.add_access(user, :read) }

View file

@ -4,7 +4,7 @@ describe Gitlab::API do
include ApiHelpers
let(:user) { create(:user) }
let!(:project) { create(:project, owner: user) }
let!(:project) { create(:project, namespace: user.namespace ) }
let!(:milestone) { create(:milestone, project: project) }
before { project.add_access(user, :read) }

View file

@ -4,7 +4,7 @@ describe Gitlab::API do
include ApiHelpers
let(:user) { create(:user) }
let!(:project) { create(:project, owner: user) }
let!(:project) { create(:project, namespace: user.namespace ) }
let!(:issue) { create(:issue, project: project, author: user) }
let!(:snippet) { create(:snippet, project: project, author: user) }
let!(:issue_note) { create(:note, noteable: issue, project: project, author: user) }

View file

@ -7,7 +7,7 @@ describe Gitlab::API do
let(:user2) { create(:user) }
let(:user3) { create(:user) }
let!(:hook) { create(:project_hook, project: project, url: "http://example.com") }
let!(:project) { create(:project, owner: user ) }
let!(:project) { create(:project, namespace: user.namespace ) }
let!(:snippet) { create(:snippet, author: user, project: project, title: 'example') }
let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) }
let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) }
@ -79,7 +79,7 @@ describe Gitlab::API do
end
it "should return a project by path name" do
get api("/projects/#{project.path}", user)
get api("/projects/#{project.id}", user)
response.status.should == 200
json_response['name'].should == project.name
end
@ -93,7 +93,7 @@ describe Gitlab::API do
describe "GET /projects/:id/repository/branches" do
it "should return an array of project branches" do
get api("/projects/#{project.path}/repository/branches", user)
get api("/projects/#{project.id}/repository/branches", user)
response.status.should == 200
json_response.should be_an Array
json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
@ -102,7 +102,7 @@ describe Gitlab::API do
describe "GET /projects/:id/repository/branches/:branch" do
it "should return the branch information for a single branch" do
get api("/projects/#{project.path}/repository/branches/new_design", user)
get api("/projects/#{project.id}/repository/branches/new_design", user)
response.status.should == 200
json_response['name'].should == 'new_design'
@ -112,7 +112,7 @@ describe Gitlab::API do
describe "GET /projects/:id/members" do
it "should return project team members" do
get api("/projects/#{project.path}/members", user)
get api("/projects/#{project.id}/members", user)
response.status.should == 200
json_response.should be_an Array
json_response.count.should == 2
@ -120,7 +120,7 @@ describe Gitlab::API do
end
it "finds team members with query string" do
get api("/projects/#{project.path}/members", user), query: user.username
get api("/projects/#{project.id}/members", user), query: user.username
response.status.should == 200
json_response.should be_an Array
json_response.count.should == 1
@ -130,7 +130,7 @@ describe Gitlab::API do
describe "GET /projects/:id/members/:user_id" do
it "should return project team member" do
get api("/projects/#{project.path}/members/#{user.id}", user)
get api("/projects/#{project.id}/members/#{user.id}", user)
response.status.should == 200
json_response['email'].should == user.email
json_response['access_level'].should == UsersProject::MASTER
@ -140,7 +140,7 @@ describe Gitlab::API do
describe "POST /projects/:id/members" do
it "should add user to project team" do
expect {
post api("/projects/#{project.path}/members", user), user_id: user2.id,
post api("/projects/#{project.id}/members", user), user_id: user2.id,
access_level: UsersProject::DEVELOPER
}.to change { UsersProject.count }.by(1)
@ -152,7 +152,7 @@ describe Gitlab::API do
describe "PUT /projects/:id/members/:user_id" do
it "should update project team member" do
put api("/projects/#{project.path}/members/#{user3.id}", user), access_level: UsersProject::MASTER
put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: UsersProject::MASTER
response.status.should == 200
json_response['email'].should == user3.email
json_response['access_level'].should == UsersProject::MASTER
@ -162,14 +162,14 @@ describe Gitlab::API do
describe "DELETE /projects/:id/members/:user_id" do
it "should remove user from project team" do
expect {
delete api("/projects/#{project.path}/members/#{user3.id}", user)
delete api("/projects/#{project.id}/members/#{user3.id}", user)
}.to change { UsersProject.count }.by(-1)
end
end
describe "GET /projects/:id/hooks" do
it "should return project hooks" do
get api("/projects/#{project.path}/hooks", user)
get api("/projects/#{project.id}/hooks", user)
response.status.should == 200
@ -181,7 +181,7 @@ describe Gitlab::API do
describe "GET /projects/:id/hooks/:hook_id" do
it "should return a project hook" do
get api("/projects/#{project.path}/hooks/#{hook.id}", user)
get api("/projects/#{project.id}/hooks/#{hook.id}", user)
response.status.should == 200
json_response['url'].should == hook.url
end
@ -190,7 +190,7 @@ describe Gitlab::API do
describe "POST /projects/:id/hooks" do
it "should add hook to project" do
expect {
post api("/projects/#{project.path}/hooks", user),
post api("/projects/#{project.id}/hooks", user),
"url" => "http://example.com"
}.to change {project.hooks.count}.by(1)
end
@ -198,7 +198,7 @@ describe Gitlab::API do
describe "PUT /projects/:id/hooks/:hook_id" do
it "should update an existing project hook" do
put api("/projects/#{project.path}/hooks/#{hook.id}", user),
put api("/projects/#{project.id}/hooks/#{hook.id}", user),
url: 'http://example.org'
response.status.should == 200
json_response['url'].should == 'http://example.org'
@ -209,7 +209,7 @@ describe Gitlab::API do
describe "DELETE /projects/:id/hooks" do
it "should delete hook from project" do
expect {
delete api("/projects/#{project.path}/hooks", user),
delete api("/projects/#{project.id}/hooks", user),
hook_id: hook.id
}.to change {project.hooks.count}.by(-1)
end
@ -217,7 +217,7 @@ describe Gitlab::API do
describe "GET /projects/:id/repository/tags" do
it "should return an array of project tags" do
get api("/projects/#{project.path}/repository/tags", user)
get api("/projects/#{project.id}/repository/tags", user)
response.status.should == 200
json_response.should be_an Array
json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
@ -229,7 +229,7 @@ describe Gitlab::API do
before { project.add_access(user2, :read) }
it "should return project commits" do
get api("/projects/#{project.path}/repository/commits", user)
get api("/projects/#{project.id}/repository/commits", user)
response.status.should == 200
json_response.should be_an Array
@ -239,7 +239,7 @@ describe Gitlab::API do
context "unauthorized user" do
it "should not return project commits" do
get api("/projects/#{project.path}/repository/commits")
get api("/projects/#{project.id}/repository/commits")
response.status.should == 401
end
end
@ -247,7 +247,7 @@ describe Gitlab::API do
describe "GET /projects/:id/snippets" do
it "should return an array of project snippets" do
get api("/projects/#{project.path}/snippets", user)
get api("/projects/#{project.id}/snippets", user)
response.status.should == 200
json_response.should be_an Array
json_response.first['title'].should == snippet.title
@ -256,7 +256,7 @@ describe Gitlab::API do
describe "GET /projects/:id/snippets/:snippet_id" do
it "should return a project snippet" do
get api("/projects/#{project.path}/snippets/#{snippet.id}", user)
get api("/projects/#{project.id}/snippets/#{snippet.id}", user)
response.status.should == 200
json_response['title'].should == snippet.title
end
@ -264,7 +264,7 @@ describe Gitlab::API do
describe "POST /projects/:id/snippets" do
it "should create a new project snippet" do
post api("/projects/#{project.path}/snippets", user),
post api("/projects/#{project.id}/snippets", user),
title: 'api test', file_name: 'sample.rb', code: 'test'
response.status.should == 201
json_response['title'].should == 'api test'
@ -273,7 +273,7 @@ describe Gitlab::API do
describe "PUT /projects/:id/snippets/:shippet_id" do
it "should update an existing project snippet" do
put api("/projects/#{project.path}/snippets/#{snippet.id}", user),
put api("/projects/#{project.id}/snippets/#{snippet.id}", user),
code: 'updated code'
response.status.should == 200
json_response['title'].should == 'example'
@ -284,31 +284,31 @@ describe Gitlab::API do
describe "DELETE /projects/:id/snippets/:snippet_id" do
it "should delete existing project snippet" do
expect {
delete api("/projects/#{project.path}/snippets/#{snippet.id}", user)
delete api("/projects/#{project.id}/snippets/#{snippet.id}", user)
}.to change { Snippet.count }.by(-1)
end
end
describe "GET /projects/:id/snippets/:snippet_id/raw" do
it "should get a raw project snippet" do
get api("/projects/#{project.path}/snippets/#{snippet.id}/raw", user)
get api("/projects/#{project.id}/snippets/#{snippet.id}/raw", user)
response.status.should == 200
end
end
describe "GET /projects/:id/:sha/blob" do
it "should get the raw file contents" do
get api("/projects/#{project.path}/repository/commits/master/blob?filepath=README.md", user)
get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
response.status.should == 200
end
it "should return 404 for invalid branch_name" do
get api("/projects/#{project.path}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
get api("/projects/#{project.id}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
response.status.should == 404
end
it "should return 404 for invalid file" do
get api("/projects/#{project.path}/repository/commits/master/blob?filepath=README.invalid", user)
get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.invalid", user)
response.status.should == 404
end
end

View file

@ -5,7 +5,7 @@ describe "Projects" do
describe "GET /projects/show" do
before do
@project = create(:project, owner: @user)
@project = create(:project, namespace: @user.namespace)
@project.add_access(@user, :read)
visit project_path(@project)
@ -37,7 +37,7 @@ describe "Projects" do
describe "PUT /projects/:id" do
before do
@project = create(:project, owner: @user)
@project = create(:project, namespace: @user.namespace)
@project.add_access(@user, :admin, :read)
visit edit_project_path(@project)
@ -58,7 +58,7 @@ describe "Projects" do
describe "DELETE /projects/:id" do
before do
@project = create(:project, owner: @user)
@project = create(:project, namespace: @user.namespace)
@project.add_access(@user, :read, :admin)
visit edit_project_path(@project)
end

View file

@ -38,7 +38,7 @@ RSpec.configure do |config|
stub_gitolite!
# !!! Observers disabled by default in tests
ActiveRecord::Base.observers.disable(:all)
#ActiveRecord::Base.observers.disable(:all)
# ActiveRecord::Base.observers.enable(:all)
# Use tmp dir for FS manipulations