Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'spec/spec_helper'
- describe Api::V1::ApiController do
- before :each do
- @puzzle = create_puzzle(create_user)
- @puzzle.update_attribute :puzzle_type, Puzzle::REBUS
- @puzzle.nominated_by(users(:admin), :reason => 'nominated')
- @puzzle.featured_by(users(:admin), :reason => 'featured', :publication_date => 1.week.ago)
- @puzzle.reload
- @user = User.first
- end
- it "should login fine with valid credentials" do
- xml_hash = process_and_parse_response :post, :login, :email_address => "joevandyk@gmail.com", :password => "asdfasdf"
- xml_hash["status"].should == "ok"
- xml_hash["username"].should == "joe"
- end
- it "should not login with invalid credentials" do
- xml_hash = process_and_parse_response :post, :login, :email_address => "joevandyk@gmail.com", :password => "asdfasd"
- xml_hash["status"].should == "failure"
- end
- it "should get info fine" do
- xml_hash = process_and_parse_response :get, :info
- ["This is a good book.", "Some free product"].each do |description|
- assert xml_hash["deals"]["deal"].find { |d| d["description"] == description }, "'#{description}' not in deals"
- end
- xml_hash["cryptopix"].should_not be_nil
- end
- it "should submit a valid answer" do
- xml_hash = process_and_parse_response :post, :already_answered, :id => @puzzle.id, :user_id => @user.id
- xml_hash["answered"].should == "false"
- assert_difference "PuzzleAnswer.count" do
- xml_hash = process_and_parse_response :post, :submit_answer, :answer => @puzzle.answer, :id => @puzzle.id, :user_id => @user.id, :time => 10
- xml_hash["status"].should == "ok"
- xml_hash["points"].should == "3"
- xml_hash["time_to_answer"].to_i.should == 10
- end
- # XXX: second submit from the same user, is it ok?
- assert_no_difference "PuzzleAnswer.count" do
- xml_hash = process_and_parse_response :post, :submit_answer, :answer => @puzzle.answer, :id => @puzzle.id, :user_id => @user.id, :time => 10
- xml_hash["status"].should == "ok"
- xml_hash["points"].should == "3"
- end
- xml_hash = process_and_parse_response :post, :already_answered, :id => @puzzle.id, :user_id => @user.id
- xml_hash["answered"].should == "true"
- end
- it "should not accept invalid answer" do
- xml_hash = process_and_parse_response :post, :submit_answer, :answer => 'ololo', :id => @puzzle.id, :user_id => @user.id
- xml_hash["status"].should == "ok"
- xml_hash["answered"].should == "false"
- xml_hash = process_and_parse_response :post, :already_answered, :id => @puzzle.id, :user_id => @user.id
- xml_hash["status"].should == "ok"
- xml_hash["answered"].should == "false"
- end
- it "should register a valid user" do
- args = { :username => 'joetheuser', :email_address => 'joe-email@tanga.com', :password => 'hellothere', :alerts => 'true', :newsletter => 'true' }
- assert_difference "User.count" do
- xml_hash = process_and_parse_response :post, :register, args
- xml_hash["status"].should == "ok"
- xml_hash["user_id"].to_i.should == User.last.id
- end
- u = User.last
- u.email_address.should == args[:email_address]
- u.username.should == args[:username]
- assert u.newsletter?
- assert u.product_email_notification?
- # Make sure they can login via the API now
- xml_hash = process_and_parse_response :post, :login, :email_address => args[:email_address], :password => args[:password]
- xml_hash["status"].should == "ok"
- end
- it "should not register an invalid user" do
- args = { :username => 'joetheuser', :email_address => 'joe-email', :password => 'hellothere', :alerts => 'true', :newsletter => 'true' }
- assert_no_difference "User.count" do
- xml_hash = process_and_parse_response :post, :register, args
- xml_hash.should == {"status"=>"failure", "message"=>"Email address is not formatted correctly"}
- end
- end
- def process_and_parse_response(method, action, parms = {})
- send method, action, parms
- check_and_parse_response(response)
- end
- # check and parse response
- def check_and_parse_response(resp)
- resp.content_type.should == "application/xml"
- Hash.from_xml(resp.body)["tanga"] # remove "tanga", to not repeat
- end
- end
Add Comment
Please, Sign In to add comment