/spec/imified_spec.rb

https://github.com/karayusuf/imified · Ruby · 153 lines · 138 code · 15 blank · 0 comment · 0 complexity · 2550bf82e6d9b22904db34fb3424ae43 MD5 · raw file

  1. require 'spec_helper'
  2. describe Imified do
  3. it "should know the Imified URL" do
  4. Imified::URL.should eql('https://www.imified.com/api/bot/')
  5. end
  6. context "when the user is specifying their accout information" do
  7. before(:each) do
  8. Imified.setup do |config|
  9. config.botkey = 'my botkey'
  10. config.email_address = 'my email address'
  11. config.password = 'my super secret password'
  12. end
  13. end
  14. it "should set the botkey to the specified value" do
  15. Imified.botkey.should eql('my botkey')
  16. end
  17. it "should set the email address to the specified value" do
  18. Imified.email_address.should eql('my email address')
  19. end
  20. it "should set the password to the specified value" do
  21. Imified.password.should eql('my super secret password')
  22. end
  23. end
  24. context "when sending a message to a user" do
  25. let(:mock_request) { double('imified_request').as_null_object }
  26. let(:mock_response) { double('imfiied_response').as_null_object }
  27. before(:each) do
  28. Imified::Request.stub(:new).and_return(mock_request)
  29. mock_request.stub(:submit).and_return(mock_response)
  30. end
  31. it "should prepare a new request to 'send'" do
  32. Imified::Request.
  33. should_receive(:new).
  34. with('send').
  35. and_return(mock_request)
  36. Imified.send_message('Rspec message', :to => 'userkey')
  37. end
  38. it "should add the specified message to the request" do
  39. mock_request.
  40. should_receive(:add_field).
  41. with('msg', 'a message to send')
  42. Imified.send_message('a message to send', :to => 'a user')
  43. end
  44. it "should add the specified userkey to the request" do
  45. mock_request.
  46. should_receive(:add_field).
  47. with('userkey', 'someones userkey')
  48. Imified.send_message('A Message', :to => 'someones userkey')
  49. end
  50. it "should lookup the userkey using the specified email" do
  51. Imified.stub!(:user_list).
  52. and_return(example_get_all_users_response)
  53. mock_request.
  54. should_receive(:add_field).
  55. with('userkey', '123456789')
  56. Imified.send_message('A Message', :to => 'me@jkarayusuf.com')
  57. end
  58. it "should submit the request" do
  59. mock_request.should_receive(:submit)
  60. Imified.send_message('A Message', :to => 'someones userkey')
  61. end
  62. end
  63. context "when fetching a list of the bot's users" do
  64. let(:mock_request) { double('imified_request').as_null_object }
  65. let(:mock_response) { double('imfiied_response').as_null_object }
  66. before(:each) do
  67. Imified::Request.stub(:new).and_return(mock_request)
  68. mock_request.stub(:submit).and_return(mock_response)
  69. end
  70. it "should prepare a new request" do
  71. Imified::Request.should_receive(:new)
  72. Imified.get_all_users
  73. end
  74. it "should submit the request" do
  75. mock_request.should_receive(:submit)
  76. Imified.get_all_users
  77. end
  78. end
  79. context "when fetching details for a specified user" do
  80. let(:mock_request) { double('imified_request').as_null_object }
  81. let(:mock_response) { double('imified_response').as_null_object }
  82. before(:each) do
  83. Imified::Request.stub(:new).and_return(mock_request)
  84. mock_request.stub(:submit).and_return(mock_response)
  85. end
  86. it "should prepare a new request to 'getuser'" do
  87. Imified::Request.
  88. should_receive(:new).
  89. with('getuser')
  90. Imified.get_user('someones userkey')
  91. end
  92. it "should add the specified userkey to the request" do
  93. mock_request.
  94. should_receive(:add_field).
  95. with('userkey', 'someones userkey')
  96. Imified.get_user('someones userkey')
  97. end
  98. it "should submit the request" do
  99. mock_request.should_receive(:submit)
  100. Imified.get_user('userkey')
  101. end
  102. end
  103. context "when validating the configuration" do
  104. it "should not raise an error if all of the values are set" do
  105. Imified.setup do |config|
  106. config.botkey = 'botkey'
  107. config.email_address = 'test email'
  108. config.password = 'super secret'
  109. end
  110. expect { Imified.validate_configuration! }.to_not raise_error(ArgumentError)
  111. end
  112. it "should raise an error if the botkey has not been specified" do
  113. Imified.stub(:botkey).and_return(nil)
  114. expect { Imified.validate_configuration! }.to raise_error(ArgumentError)
  115. end
  116. it "should raise an error if the email address has not been specified" do
  117. Imified.stub(:email_address).and_return(nil)
  118. expect { Imified.validate_configuration! }.to raise_error(ArgumentError)
  119. end
  120. it "should raise an error if the password has not been specified" do
  121. Imified.stub(:password).and_return(nil)
  122. expect { Imified.validate_configuration! }.to raise_error(ArgumentError)
  123. end
  124. end
  125. end