mirror of
https://github.com/TracksApp/tracks.git
synced 2026-02-11 18:04:22 +01:00
Rename new_context and new_project actions to create to fit into the CRUD model, add tests for these methods, too.
Add a plugin to allow executing tests using an in-memory sqlite database. Faster! git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@318 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
parent
3e22381187
commit
ba3757f29e
29 changed files with 416 additions and 114 deletions
|
|
@ -9,42 +9,40 @@ class UserController; def rescue_action(e) raise e end; end
|
|||
class CreateUserControllerTest < ActionController::IntegrationTest
|
||||
fixtures :users
|
||||
|
||||
@@foobar_postdata = "<request><login>foo</login><password>bar</password></request>"
|
||||
@@john_postdata = "<request><login>john</login><password>barracuda</password></request>"
|
||||
|
||||
def setup
|
||||
assert_equal "test", ENV['RAILS_ENV']
|
||||
assert_equal "change-me", User.get_salt()
|
||||
@foobar_postdata = "<request><login>foo</login><password>bar</password></request>"
|
||||
@john_postdata = "<request><login>john</login><password>barracuda</password></request>"
|
||||
assert_test_environment_ok
|
||||
end
|
||||
|
||||
def test_fails_with_401_if_not_authorized_user
|
||||
authenticated_post_xml '/user/create', 'nobody', 'nohow', @foobar_postdata
|
||||
authenticated_post_xml_to_user_create @@foobar_postdata, 'nobody', 'nohow'
|
||||
assert_401_unauthorized
|
||||
end
|
||||
|
||||
def test_fails_with_401_if_not_admin_user
|
||||
authenticated_post_xml '/user/create', users(:other_user).login, 'sesame', @foobar_postdata
|
||||
authenticated_post_xml_to_user_create @@foobar_postdata, users(:other_user).login, 'sesame'
|
||||
assert_401_unauthorized
|
||||
end
|
||||
|
||||
def test_content_type_must_be_xml
|
||||
authenticated_post_xml "/user/create", users(:admin_user).login, 'abracadabra', @foobar_postdata, {'CONTENT_TYPE' => "application/x-www-form-urlencoded"}
|
||||
authenticated_post_xml_to_user_create @@foobar_postdata, users(:admin_user).login, 'abracadabra', {'CONTENT_TYPE' => "application/x-www-form-urlencoded"}
|
||||
assert_response_and_body 404, "Content Type must be application/xml."
|
||||
end
|
||||
|
||||
def test_fails_with_invalid_xml_format
|
||||
invalid_postdata = "<foo></bar>"
|
||||
authenticated_post_xml "/user/create", users(:admin_user).login, 'abracadabra', invalid_postdata
|
||||
authenticated_post_xml_to_user_create "<foo></bar>"
|
||||
assert_404_invalid_xml
|
||||
end
|
||||
|
||||
def test_fails_with_invalid_xml_format2
|
||||
invalid_postdata = "<request><username>foo</username></request>"
|
||||
authenticated_post_xml "/user/create", users(:admin_user).login, 'abracadabra', invalid_postdata
|
||||
authenticated_post_xml_to_user_create "<request><username>foo</username></request>"
|
||||
assert_404_invalid_xml
|
||||
end
|
||||
|
||||
def test_xml_simple_param_parsing
|
||||
authenticated_post_xml "/user/create", users(:admin_user).login, 'abracadabra', @foobar_postdata
|
||||
authenticated_post_xml_to_user_create
|
||||
assert @controller.params.has_key?(:request)
|
||||
assert @controller.params[:request].has_key?(:login)
|
||||
assert @controller.params[:request].has_key?(:password)
|
||||
|
|
@ -53,21 +51,21 @@ class CreateUserControllerTest < ActionController::IntegrationTest
|
|||
end
|
||||
|
||||
def test_fails_with_too_short_password
|
||||
authenticated_post_xml "/user/create", users(:admin_user).login, 'abracadabra', @foobar_postdata
|
||||
authenticated_post_xml_to_user_create
|
||||
assert_response_and_body 404, "Password is too short (minimum is 5 characters)"
|
||||
end
|
||||
|
||||
def test_fails_with_nonunique_login
|
||||
existing_login = users(:other_user).login
|
||||
data = "<request><login>#{existing_login}</login><password>barracuda</password></request>"
|
||||
authenticated_post_xml "/user/create", users(:admin_user).login, 'abracadabra', data
|
||||
authenticated_post_xml_to_user_create "<request><login>#{existing_login}</login><password>barracuda</password></request>"
|
||||
assert_response_and_body 404, "Login has already been taken"
|
||||
end
|
||||
|
||||
def test_creates_new_user
|
||||
authenticated_post_xml '/user/create', users(:admin_user).login, 'abracadabra', @john_postdata
|
||||
initial_count = User.count
|
||||
authenticated_post_xml_to_user_create @@john_postdata
|
||||
assert_response_and_body 200, "User created."
|
||||
assert_equal 3, User.count
|
||||
assert_equal initial_count + 1, User.count
|
||||
john1 = User.find_by_login('john')
|
||||
assert_not_nil john1, "expected user john to be created"
|
||||
john2 = User.authenticate('john','barracuda')
|
||||
|
|
@ -79,37 +77,13 @@ class CreateUserControllerTest < ActionController::IntegrationTest
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def authenticated_post_xml(url, username, password, parameters, headers = {})
|
||||
post url,
|
||||
parameters,
|
||||
{'AUTHORIZATION' => "Basic " + Base64.encode64("#{username}:#{password}"),
|
||||
'ACCEPT' => 'application/xml',
|
||||
'CONTENT_TYPE' => 'application/xml'
|
||||
}.merge(headers)
|
||||
end
|
||||
|
||||
def authenticated_get_xml(url, username, password, parameters, headers = {})
|
||||
get url,
|
||||
parameters,
|
||||
{'AUTHORIZATION' => "Basic " + Base64.encode64("#{username}:#{password}"),
|
||||
'ACCEPT' => 'application/xml',
|
||||
'CONTENT_TYPE' => 'application/xml'
|
||||
}.merge(headers)
|
||||
end
|
||||
|
||||
def assert_401_unauthorized
|
||||
assert_response_and_body 401, "401 Unauthorized: You are not authorized to interact with Tracks."
|
||||
def authenticated_post_xml_to_user_create(postdata = @@foobar_postdata, user = users(:admin_user).login, password = 'abracadabra', headers = {})
|
||||
authenticated_post_xml "/user/create", user, password, postdata, headers
|
||||
end
|
||||
|
||||
def assert_404_invalid_xml
|
||||
assert_response_and_body 404, "Expected post format is xml like so: <request><login>username</login><password>abc123</password></request>."
|
||||
end
|
||||
|
||||
def assert_response_and_body (type, body, message = nil)
|
||||
#puts @response.body
|
||||
assert_response type, message
|
||||
assert_equal body, @response.body, message
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue