Guest User

Untitled

a guest
Apr 8th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. require 'spec/spec_helper'
  2.  
  3. describe Api::V1::ApiController do
  4.  
  5. before :each do
  6. @puzzle = create_puzzle(create_user)
  7. @puzzle.update_attribute :puzzle_type, Puzzle::REBUS
  8. @puzzle.nominated_by(users(:admin), :reason => 'nominated')
  9. @puzzle.featured_by(users(:admin), :reason => 'featured', :publication_date => 1.week.ago)
  10. @puzzle.reload
  11.  
  12. @user = User.first
  13. end
  14.  
  15. it "should login fine with valid credentials" do
  16. xml_hash = process_and_parse_response :post, :login, :email_address => "joevandyk@gmail.com", :password => "asdfasdf"
  17.  
  18. xml_hash["status"].should == "ok"
  19. xml_hash["username"].should == "joe"
  20. end
  21.  
  22. it "should not login with invalid credentials" do
  23. xml_hash = process_and_parse_response :post, :login, :email_address => "joevandyk@gmail.com", :password => "asdfasd"
  24. xml_hash["status"].should == "failure"
  25. end
  26.  
  27. it "should get info fine" do
  28. xml_hash = process_and_parse_response :get, :info
  29.  
  30. ["This is a good book.", "Some free product"].each do |description|
  31. assert xml_hash["deals"]["deal"].find { |d| d["description"] == description }, "'#{description}' not in deals"
  32. end
  33. xml_hash["cryptopix"].should_not be_nil
  34. end
  35.  
  36. it "should submit a valid answer" do
  37. xml_hash = process_and_parse_response :post, :already_answered, :id => @puzzle.id, :user_id => @user.id
  38. xml_hash["answered"].should == "false"
  39.  
  40. assert_difference "PuzzleAnswer.count" do
  41. xml_hash = process_and_parse_response :post, :submit_answer, :answer => @puzzle.answer, :id => @puzzle.id, :user_id => @user.id, :time => 10
  42. xml_hash["status"].should == "ok"
  43. xml_hash["points"].should == "3"
  44. xml_hash["time_to_answer"].to_i.should == 10
  45. end
  46.  
  47. # XXX: second submit from the same user, is it ok?
  48. assert_no_difference "PuzzleAnswer.count" do
  49. xml_hash = process_and_parse_response :post, :submit_answer, :answer => @puzzle.answer, :id => @puzzle.id, :user_id => @user.id, :time => 10
  50. xml_hash["status"].should == "ok"
  51. xml_hash["points"].should == "3"
  52. end
  53.  
  54. xml_hash = process_and_parse_response :post, :already_answered, :id => @puzzle.id, :user_id => @user.id
  55. xml_hash["answered"].should == "true"
  56. end
  57.  
  58. it "should not accept invalid answer" do
  59. xml_hash = process_and_parse_response :post, :submit_answer, :answer => 'ololo', :id => @puzzle.id, :user_id => @user.id
  60. xml_hash["status"].should == "ok"
  61. xml_hash["answered"].should == "false"
  62.  
  63. xml_hash = process_and_parse_response :post, :already_answered, :id => @puzzle.id, :user_id => @user.id
  64. xml_hash["status"].should == "ok"
  65. xml_hash["answered"].should == "false"
  66. end
  67.  
  68. it "should register a valid user" do
  69. args = { :username => 'joetheuser', :email_address => 'joe-email@tanga.com', :password => 'hellothere', :alerts => 'true', :newsletter => 'true' }
  70. assert_difference "User.count" do
  71. xml_hash = process_and_parse_response :post, :register, args
  72. xml_hash["status"].should == "ok"
  73. xml_hash["user_id"].to_i.should == User.last.id
  74. end
  75. u = User.last
  76. u.email_address.should == args[:email_address]
  77. u.username.should == args[:username]
  78. assert u.newsletter?
  79. assert u.product_email_notification?
  80.  
  81. # Make sure they can login via the API now
  82. xml_hash = process_and_parse_response :post, :login, :email_address => args[:email_address], :password => args[:password]
  83. xml_hash["status"].should == "ok"
  84. end
  85.  
  86. it "should not register an invalid user" do
  87. args = { :username => 'joetheuser', :email_address => 'joe-email', :password => 'hellothere', :alerts => 'true', :newsletter => 'true' }
  88. assert_no_difference "User.count" do
  89. xml_hash = process_and_parse_response :post, :register, args
  90. xml_hash.should == {"status"=>"failure", "message"=>"Email address is not formatted correctly"}
  91. end
  92. end
  93.  
  94. def process_and_parse_response(method, action, parms = {})
  95. send method, action, parms
  96. check_and_parse_response(response)
  97. end
  98.  
  99. # check and parse response
  100. def check_and_parse_response(resp)
  101. resp.content_type.should == "application/xml"
  102. Hash.from_xml(resp.body)["tanga"] # remove "tanga", to not repeat
  103. end
  104. end
Add Comment
Please, Sign In to add comment