PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/controllers/oauth_controller_spec.rb

https://github.com/rickjames/teambox
Ruby | 364 lines | 294 code | 70 blank | 0 comment | 29 complexity | 4b71e5fc200dd541dacffbc8fc43e78c MD5 | raw file
  1. require 'spec_helper'
  2. require 'json'
  3. describe OauthController do
  4. fixtures :oauth_tokens
  5. describe "2.0 authorization code flow" do
  6. before(:each) do
  7. login
  8. end
  9. describe "authorize redirect" do
  10. before(:each) do
  11. get :authorize, :response_type=>"code",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback"
  12. end
  13. it "should render authorize" do
  14. response.should render_template("authorize")
  15. end
  16. it "should not create token" do
  17. Oauth2Verifier.last.should be_nil
  18. end
  19. end
  20. describe "authorize invalid client" do
  21. it "should render a failure page if an invalid client is used" do
  22. get :authorize, :response_type=>"code",:client_id=>'000', :redirect_uri=>"http://application/callback"
  23. response.body.should == 'Invalid Application Key'
  24. end
  25. end
  26. describe "authorize invalid response type" do
  27. it "should render a failure page if an invalid response type is used" do
  28. get :authorize, :response_type=>"fudge",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback"
  29. response.body.should == 'Invalid Request'
  30. end
  31. end
  32. describe "authorize" do
  33. before(:each) do
  34. post :authorize, :response_type=>"code",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback",:authorize=>"1"
  35. @verification_token = Oauth2Verifier.last
  36. @oauth2_token_count= Oauth2Token.count
  37. end
  38. subject { @verification_token }
  39. it { should_not be_nil }
  40. it "should set user on verification token" do
  41. @verification_token.user.should==current_user
  42. end
  43. it "should set redirect_url" do
  44. @verification_token.redirect_url.should == "http://application/callback"
  45. end
  46. it "should redirect to default callback" do
  47. response.should be_redirect
  48. uri = URI.parse(response.redirect_url)
  49. query = Rack::Utils.parse_query(uri.query)
  50. uri.host.should == 'application'
  51. uri.path.should == '/callback'
  52. query['code'].should == @verification_token.code
  53. end
  54. describe "get token" do
  55. before(:each) do
  56. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  57. @token = Oauth2Token.last
  58. end
  59. subject { @token }
  60. it { should_not be_nil }
  61. it { should be_authorized }
  62. it "should have added a new token" do
  63. Oauth2Token.count.should==@oauth2_token_count+1
  64. end
  65. it "should have cleared the verification token" do
  66. Oauth2Verifier.find_by_token(@verification_token.token).should == nil
  67. end
  68. it "should set user to current user" do
  69. @token.user.should==current_user
  70. end
  71. it "should return json token" do
  72. data = JSON.parse(response.body)
  73. data["access_token"].should==@token.token
  74. end
  75. end
  76. describe "get token with the same verifier twice fails" do
  77. before(:each) do
  78. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  79. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  80. end
  81. it "should return incorrect_client_credentials error" do
  82. JSON.parse(response.body).should == {"error"=>"invalid_grant"}
  83. end
  84. end
  85. describe "get token twice destroys existing access tokens" do
  86. before(:each) do
  87. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  88. @token = Oauth2Token.last
  89. post :authorize, :response_type=>"code",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback",:authorize=>"1"
  90. @verification_token = Oauth2Verifier.last
  91. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  92. @new_token = Oauth2Token.last
  93. end
  94. it "should generate a new token" do
  95. response.should be_success
  96. @token.id.should_not == @new_token.id
  97. end
  98. it "should destroy the old token" do
  99. Oauth2Token.find_by_id(@token.id).should == nil
  100. Oauth2Token.find_by_id(@new_token.id).should_not == nil
  101. end
  102. end
  103. describe "get token with wrong secret" do
  104. before(:each) do
  105. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>"fake", :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  106. end
  107. it "should not create token" do
  108. Oauth2Token.count.should==@oauth2_token_count
  109. end
  110. it "should return incorrect_client_credentials error" do
  111. JSON.parse(response.body).should == {"error"=>"invalid_client"}
  112. end
  113. end
  114. describe "get token with wrong code" do
  115. before(:each) do
  116. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>"fake"
  117. end
  118. it "should not create token" do
  119. Oauth2Token.count.should==@oauth2_token_count
  120. end
  121. it "should return incorrect_client_credentials error" do
  122. JSON.parse(response.body).should == {"error"=>"invalid_grant"}
  123. end
  124. end
  125. describe "get token with wrong redirect_url" do
  126. before(:each) do
  127. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://evil/callback",:code=>@verification_token.code
  128. end
  129. it "should not create token" do
  130. Oauth2Token.count.should==@oauth2_token_count
  131. end
  132. it "should return incorrect_client_credentials error" do
  133. JSON.parse(response.body).should == {"error"=>"invalid_grant"}
  134. end
  135. end
  136. end
  137. describe "scopes on authorize" do
  138. def auth_with_scope(scope)
  139. post :authorize, :response_type=>"code",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback",:authorize=>"1", :scope => scope
  140. @verification_token = Oauth2Verifier.last
  141. end
  142. it "should only allow OauthToken::ALLOWED_SCOPES" do
  143. auth_with_scope("offline_access github")
  144. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code
  145. @token = Oauth2Token.last
  146. @token.scope.should == [:offline_access]
  147. end
  148. it "should allow the scope to be further restricted on token" do
  149. auth_with_scope("offline_access read_projects")
  150. post :token, :grant_type=>"authorization_code", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :redirect_uri=>"http://application/callback",:code=>@verification_token.code, :scope => "read_projects write_projects"
  151. @token = Oauth2Token.last
  152. @token.scope.should == [:read_projects]
  153. end
  154. end
  155. describe "redirect_uri on authorize" do
  156. def auth_with_redirect(url)
  157. post :authorize, :response_type=>"code",:client_id=>current_client_application.key, :redirect_uri=>url,:authorize=>"1"
  158. @verification_token = Oauth2Verifier.last
  159. end
  160. it "should not allow a blank uri" do
  161. post :authorize, :response_type=>"code",:client_id=>current_client_application.key, :authorize=>"1"
  162. response.should be_redirect
  163. uri = URI.parse(response.redirect_url)
  164. query = Rack::Utils.parse_query(uri.query)
  165. uri.host.should == 'application'
  166. uri.path.should == '/callback'
  167. query['error'].should == 'redirect_uri_mismatch'
  168. end
  169. it "should allow http://application/callback" do
  170. auth_with_redirect("http://application/callback")
  171. response.should be_redirect
  172. end
  173. it "should not allow http://other-application/callback" do
  174. auth_with_redirect("http://other-application/callback")
  175. uri = URI.parse(response.redirect_url)
  176. query = Rack::Utils.parse_query(uri.query)
  177. uri.host.should == 'application'
  178. uri.path.should == '/callback'
  179. query['error'].should == 'redirect_uri_mismatch'
  180. end
  181. it "should not allow http://other-application/callback on GET" do
  182. get :authorize, :response_type=>"code", :client_id=>current_client_application.key, :redirect_uri => 'http://other-application/callback'
  183. response.body.should == 'Invalid Redirect URI'
  184. end
  185. end
  186. describe "deny" do
  187. before(:each) do
  188. post :authorize, :response_type=>"code", :client_id=>current_client_application.key, :redirect_uri=>"http://application/callback",:authorize=>"0"
  189. end
  190. it { Oauth2Verifier.last.should be_nil }
  191. it "should redirect to default callback" do
  192. response.should be_redirect
  193. response.should redirect_to("http://application/callback?error=user_denied")
  194. end
  195. end
  196. end
  197. describe "2.0 authorization token flow" do
  198. before(:each) do
  199. login
  200. current_client_application # load up so it creates its own token
  201. @oauth2_token_count= Oauth2Token.count
  202. end
  203. describe "authorize redirect" do
  204. before(:each) do
  205. get :authorize, :response_type=>"token",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback"
  206. end
  207. it "should render authorize" do
  208. response.should render_template("authorize")
  209. end
  210. it "should not create token" do
  211. Oauth2Verifier.last.should be_nil
  212. end
  213. end
  214. describe "authorize" do
  215. before(:each) do
  216. post :authorize, :response_type=>"token",:client_id=>current_client_application.key, :redirect_uri=>"http://application/callback",:authorize=>"1"
  217. @token = Oauth2Token.last
  218. end
  219. subject { @token }
  220. it "should redirect to default callback" do
  221. response.should be_redirect
  222. response.should redirect_to("http://application/callback##{@token.to_fragment_params}")
  223. end
  224. it "should not have a scope" do
  225. @token.scope.should be_empty
  226. end
  227. it { should_not be_nil }
  228. it { should be_authorized }
  229. it "should set user to current user" do
  230. @token.user.should==current_user
  231. end
  232. it "should have added a new token" do
  233. Oauth2Token.count.should==@oauth2_token_count+1
  234. end
  235. end
  236. describe "deny" do
  237. before(:each) do
  238. post :authorize, :response_type=>"token", :client_id=>current_client_application.key, :redirect_uri=>"http://application/callback",:authorize=>"0"
  239. end
  240. it { Oauth2Verifier.last.should be_nil }
  241. it "should redirect to default callback" do
  242. response.should be_redirect
  243. response.should redirect_to("http://application/callback?error=user_denied")
  244. end
  245. end
  246. end
  247. describe "oauth2 token for basic credentials" do
  248. before(:each) do
  249. current_client_application
  250. @oauth2_token_count = Oauth2Token.count
  251. current_user.should_not == nil
  252. post :token, :grant_type=>"password", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :username=>current_user.login, :password=>"dragons"
  253. @token = Oauth2Token.last
  254. end
  255. it { @token.should_not be_nil }
  256. it { @token.should be_authorized }
  257. it "should set user to client_applications user" do
  258. @token.user.should==current_user
  259. end
  260. it "should have added a new token" do
  261. Oauth2Token.count.should==@oauth2_token_count+1
  262. end
  263. it "should return json token" do
  264. data = JSON.parse(response.body)
  265. data["access_token"].should==@token.token
  266. end
  267. end
  268. describe "oauth2 token for basic credentials with wrong password" do
  269. before(:each) do
  270. current_client_application
  271. @oauth2_token_count = Oauth2Token.count
  272. post :token, :grant_type=>"password", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :username=>current_user.login, :password=>"bad"
  273. end
  274. it "should not have added a new token" do
  275. Oauth2Token.count.should==@oauth2_token_count
  276. end
  277. it "should return json token" do
  278. JSON.parse(response.body).should=={"error"=>"invalid_grant"}
  279. end
  280. end
  281. describe "oauth2 token for basic credentials with unknown user" do
  282. before(:each) do
  283. current_client_application
  284. @oauth2_token_count = Oauth2Token.count
  285. post :token, :grant_type=>"password", :client_id=>current_client_application.key,:client_secret=>current_client_application.secret, :username=>"non existent", :password=>"dragons"
  286. end
  287. it "should not have added a new token" do
  288. Oauth2Token.count.should==@oauth2_token_count
  289. end
  290. it "should return json token" do
  291. JSON.parse(response.body).should=={"error"=>"invalid_grant"}
  292. end
  293. end
  294. end