Skip to content

Commit

Permalink
Rewrite grack usage of origin url and tests [#76]
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarrasch committed May 4, 2012
1 parent d7ccc33 commit e2dffec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
35 changes: 23 additions & 12 deletions lib/grack/water_grack_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ def halt(message)
def call(env)
@env = env

path_info = env["PATH_INFO"]
# We want to do auth things first
@req = Rack::Request.new(env)

values = {}
return unauthorized unless auth.provided?
return bad_request unless auth.basic?

# /courses/1/labs/2 => {:courses=>1, :labs=>2}
path_info = env["PATH_INFO"]
values = {}
# /courses/1/lab_groups/3/labs/2 => {:courses=>1, :labs=>2, :lab_groups=>3}
path_info.scan(%r{\w+/\d+}) do |match|
res = match.split("/")
values.merge!(res.first.to_sym => res.last.to_i)
Expand All @@ -51,26 +55,33 @@ def call(env)
return bad_request
end

# TODO: fix query, it should not pick first, rather based on credentials
unless lhg = lab.lab_has_groups.first
lab_group = LabGroup.
where("lab_groups.given_course_id = ?", values[:courses]).
find_by_number(values[:lab_groups])

unless lab_group
halt("Lab group not found")
return bad_request
end

# TODO: Fix most inefficient query ever

This comment has been minimized.

Copy link
@Tarrasch

Tarrasch May 4, 2012

Author Contributor

Hur enkelt är det att göra en riktig databas join nedan? Ni som kan rails, @oleander @jesjos

This comment has been minimized.

Copy link
@jesjos

jesjos May 5, 2012

Member

Som jag fattar det så vill du ha LHG:n för kombination av labb och grupp?

Kanske såhär?

lab.lab_has_groups.where(lab_group_id: lab_group.id).first

This comment has been minimized.

Copy link
@Tarrasch

Tarrasch May 6, 2012

Author Contributor

Tack, jag ska testa implementera det sen.

lab_has_groups = lab_group.lab_has_groups & lab.lab_has_groups
lab_has_group = lab_has_groups.first

unless lab_has_group
halt("Lab has group not found")
return bad_request
end

unless repository = lhg.repository
unless repository = lab_has_group.repository
halt("Repository not found")
return bad_request
end

env["PATH_INFO"] = path_info.gsub(/^.*\.git/, "/" + repository.hashed_path + ".git")

@req = Rack::Request.new(env)

return unauthorized unless auth.provided?
return bad_request unless auth.basic?
return unauthorized unless authorized?(lhg)
return unauthorized unless authorized?(lab_has_group)

# env['REMOTE_USER'] = auth.username
@app.call(env)
end

Expand Down
18 changes: 10 additions & 8 deletions spec/grack/water_grack_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def r
let(:student) { create(:student) }
let(:user) { student.user }
let(:given_course) { lab_group.given_course }
let(:url) { make_url(given_course, lab) }
let(:url) { make_url(given_course, lab, lab_group) }

def make_url(given_course, lab)
def make_url(given_course, lab, lab_group)
%W{courses/#{given_course.id}/
labs/#{lab.number}.git
labs/#{lab.number}/
lab_groups/#{lab_group.number}
.git
}.join ""
end

Expand Down Expand Up @@ -118,7 +120,7 @@ def mutate_head!
lab_group_2.add_student(student_2)
mutate_head!
head_for(user).should =~ /my_branch/
head_for(student_2.user).should =~ /master/
head_general(student_2.user, make_url(given_course, lab, lab_group_2)).should =~ /master/
end

it "redirects to correct repo when there are many labs" do
Expand All @@ -131,8 +133,8 @@ def mutate_head!
lab_group: lab_group
)
mutate_head!
head_at(make_url(given_course, lab)).should =~ /my_branch/
head_at(make_url(given_course, lab_2)).should =~ /master/
head_at(make_url(given_course, lab, lab_group)).should =~ /my_branch/
head_at(make_url(given_course, lab_2, lab_group)).should =~ /master/
end

it "redirect to correct repo throughout courses" do
Expand All @@ -148,8 +150,8 @@ def mutate_head!
lab_group_2 = lab_has_group_2.lab_group
lab_group_2.add_student(student)
mutate_head!
head_at(make_url(given_course, lab)).should =~ /my_branch/
head_at(make_url(given_course_2, lab_2)).should =~ /master/
head_at(make_url(given_course, lab, lab_group)).should =~ /my_branch/
head_at(make_url(given_course_2, lab_2, lab_group_2)).should =~ /master/
end

end
Expand Down

0 comments on commit e2dffec

Please sign in to comment.