support http basic auth for grade exchange

Change-Id: Ie61bc71ee82bc3c019dd25a9b58e7ff13aa44f54
Reviewed-on: https://gerrit.instructure.com/5049
Reviewed-by: Cody Cutrer <cody@instructure.com>
Tested-by: Hudson <hudson@instructure.com>
This commit is contained in:
JT Olds 2011-08-11 10:51:57 -06:00
parent b365936781
commit 40973e156d
4 changed files with 91 additions and 28 deletions

View File

@ -36,6 +36,7 @@ class SSLCommon
url = URI.parse(url)
http = self.get_http_conn(url.host, url.port, url.scheme.downcase == 'https')
req = Net::HTTP::Post.new(url.request_uri, headers)
req.basic_auth url.user, url.password if url.user
req.form_data = form_data if form_data
http.start {|http| http.request(req, payload) }
end

View File

@ -0,0 +1,62 @@
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe SSLCommon do
it "should work with http basic auth, username and password" do
server, server_thread, post_lines = start_test_http_server
SSLCommon.post_data("http://theusername:thepassword@localhost:#{server.addr[1]}/endpoint",
"somedata", "application/x-jt-is-so-cool")
server_thread.join
post_lines.should == [
"POST /endpoint HTTP/1.1",
"Accept: */*",
"Content-Type: application/x-jt-is-so-cool",
"Authorization: Basic #{Base64.encode64("theusername:thepassword").strip}",
"",
"somedata"]
end
it "should work with http basic auth, just username" do
server, server_thread, post_lines = start_test_http_server
SSLCommon.post_data("http://theusername@localhost:#{server.addr[1]}/endpoint",
"somedata", "application/x-jt-is-so-cool")
server_thread.join
post_lines.should == [
"POST /endpoint HTTP/1.1",
"Accept: */*",
"Content-Type: application/x-jt-is-so-cool",
"Authorization: Basic #{Base64.encode64("theusername:").strip}",
"",
"somedata"]
end
it "should work with no auth" do
server, server_thread, post_lines = start_test_http_server
SSLCommon.post_data("http://localhost:#{server.addr[1]}/endpoint",
"somedata", "application/x-jt-is-so-cool")
server_thread.join
post_lines.should == [
"POST /endpoint HTTP/1.1",
"Accept: */*",
"Content-Type: application/x-jt-is-so-cool",
"",
"somedata"]
end
end

View File

@ -655,30 +655,6 @@ describe Course, 'grade_publishing' do
PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no"
end
def start_server
post_lines = []
server = TCPServer.open(0)
port = server.addr[1]
post_lines = []
server_thread = Thread.new(server, post_lines) do |server, post_lines|
client = server.accept
content_length = 0
loop do
line = client.readline
post_lines << line.strip unless line =~ /\AHost: localhost:|\AContent-Length: /
content_length = line.split(":")[1].to_i if line.strip =~ /\AContent-Length: [0-9]+\z/
if line.strip.blank?
post_lines << client.read(content_length)
break
end
end
client.puts("HTTP/1.1 200 OK\nContent-Length: 0\n\n")
client.close
server.close
end
return server, server_thread, post_lines
end
it 'should pass a quick sanity check' do
user = User.new
Course.valid_grade_export_types["test_export"] = {
@ -691,7 +667,7 @@ describe Course, 'grade_publishing' do
PluginSetting.settings_for_plugin('grade_export')[:enabled] = "true"
PluginSetting.settings_for_plugin('grade_export')[:format_type] = "test_export"
PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no"
server, server_thread, post_lines = start_server
server, server_thread, post_lines = start_test_http_server
PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint"
@course.grading_standard_id = 0
@ -710,7 +686,7 @@ describe Course, 'grade_publishing' do
PluginSetting.settings_for_plugin('grade_export')[:enabled] = "true"
PluginSetting.settings_for_plugin('grade_export')[:format_type] = "instructure_csv"
PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no"
server, server_thread, post_lines = start_server
server, server_thread, post_lines = start_test_http_server
PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint"
@course.grading_standard_id = 0
@course.publish_final_grades(user)
@ -823,7 +799,7 @@ describe Course, 'grade_publishing' do
PluginSetting.settings_for_plugin('grade_export')[:enabled] = "true"
PluginSetting.settings_for_plugin('grade_export')[:format_type] = "instructure_csv"
PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no"
server, server_thread, post_lines = start_server
server, server_thread, post_lines = start_test_http_server
PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint"
@course.publish_final_grades(teacher.user)
server_thread.join
@ -842,7 +818,7 @@ describe Course, 'grade_publishing' do
"#{teacher.id},T1,#{sec4.id},,#{stud6.id},,#{Enrollment.find_by_user_id_and_course_section_id(stud6.user.id, sec4.id).id},active,\"\",90\n"]
@course.grading_standard_id = 0
@course.save
server, server_thread, post_lines = start_server
server, server_thread, post_lines = start_test_http_server
PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint"
@course.publish_final_grades(teacher.user)
server_thread.join

View File

@ -292,4 +292,28 @@ Spec::Runner.configure do |config|
ActionController::Base.class_eval { alias_method :allow_forgery_protection, :_old_protect }
end
def start_test_http_server
post_lines = []
server = TCPServer.open(0)
port = server.addr[1]
post_lines = []
server_thread = Thread.new(server, post_lines) do |server, post_lines|
client = server.accept
content_length = 0
loop do
line = client.readline
post_lines << line.strip unless line =~ /\AHost: localhost:|\AContent-Length: /
content_length = line.split(":")[1].to_i if line.strip =~ /\AContent-Length: [0-9]+\z/
if line.strip.blank?
post_lines << client.read(content_length)
break
end
end
client.puts("HTTP/1.1 200 OK\nContent-Length: 0\n\n")
client.close
server.close
end
return server, server_thread, post_lines
end
end