1 # frozen_string_literal: true
 
   5 class Oauth2ApplicationsControllerTest < ActionDispatch::IntegrationTest
 
   7   # test all routes which lead to this controller
 
  10       { :path => "/oauth2/applications", :method => :get },
 
  11       { :controller => "oauth2_applications", :action => "index" }
 
  14       { :path => "/oauth2/applications", :method => :post },
 
  15       { :controller => "oauth2_applications", :action => "create" }
 
  18       { :path => "/oauth2/applications/new", :method => :get },
 
  19       { :controller => "oauth2_applications", :action => "new" }
 
  22       { :path => "/oauth2/applications/1/edit", :method => :get },
 
  23       { :controller => "oauth2_applications", :action => "edit", :id => "1" }
 
  26       { :path => "/oauth2/applications/1", :method => :get },
 
  27       { :controller => "oauth2_applications", :action => "show", :id => "1" }
 
  30       { :path => "/oauth2/applications/1", :method => :patch },
 
  31       { :controller => "oauth2_applications", :action => "update", :id => "1" }
 
  34       { :path => "/oauth2/applications/1", :method => :put },
 
  35       { :controller => "oauth2_applications", :action => "update", :id => "1" }
 
  38       { :path => "/oauth2/applications/1", :method => :delete },
 
  39       { :controller => "oauth2_applications", :action => "destroy", :id => "1" }
 
  45     create_list(:oauth_application, 2, :owner => user)
 
  47     get oauth_applications_path
 
  48     assert_redirected_to login_path(:referer => oauth_applications_path)
 
  52     get oauth_applications_path
 
  53     assert_response :success
 
  54     assert_template "oauth2_applications/index"
 
  55     assert_select "tbody tr", 2
 
  58   def test_index_with_moderator_app
 
  60     create(:oauth_application, :owner => user, :scopes => "write_redactions")
 
  64     get oauth_applications_path
 
  65     assert_response :success
 
  71     get new_oauth_application_path
 
  72     assert_redirected_to login_path(:referer => new_oauth_application_path)
 
  76     get new_oauth_application_path
 
  77     assert_response :success
 
  78     assert_template "oauth2_applications/new"
 
  79     assert_select "#content form", 1 do
 
  80       assert_select "input#oauth2_application_name", 1
 
  81       assert_select "textarea#oauth2_application_redirect_uri", 1
 
  82       assert_select "input#oauth2_application_confidential", 1
 
  83       Oauth.scopes.each do |scope|
 
  84         assert_select "input#oauth2_application_scopes_#{scope.name}", 1
 
  92     assert_difference "Doorkeeper::Application.count", 0 do
 
  93       post oauth_applications_path
 
  95     assert_response :forbidden
 
  99     assert_difference "Doorkeeper::Application.count", 0 do
 
 100       post oauth_applications_path(:oauth2_application => {
 
 101                                      :name => "Test Application"
 
 104     assert_response :success
 
 105     assert_template "oauth2_applications/new"
 
 107     assert_difference "Doorkeeper::Application.count", 0 do
 
 108       post oauth_applications_path(:oauth2_application => {
 
 109                                      :name => "Test Application",
 
 110                                      :redirect_uri => "https://test.example.com/",
 
 111                                      :scopes => ["bad_scope"]
 
 114     assert_response :success
 
 115     assert_template "oauth2_applications/new"
 
 117     assert_difference "Doorkeeper::Application.count", 1 do
 
 118       post oauth_applications_path(:oauth2_application => {
 
 119                                      :name => "Test Application",
 
 120                                      :redirect_uri => "https://test.example.com/",
 
 121                                      :scopes => ["read_prefs"]
 
 124     assert_redirected_to oauth_application_path(:id => Doorkeeper::Application.find_by(:name => "Test Application").id)
 
 127   def test_create_privileged
 
 128     session_for(create(:user))
 
 130     assert_difference "Doorkeeper::Application.count", 0 do
 
 131       post oauth_applications_path(:oauth2_application => {
 
 132                                      :name => "Test Application",
 
 133                                      :redirect_uri => "https://test.example.com/",
 
 134                                      :scopes => ["read_email"]
 
 137     assert_response :success
 
 138     assert_template "oauth2_applications/new"
 
 140     session_for(create(:administrator_user))
 
 142     assert_difference "Doorkeeper::Application.count", 1 do
 
 143       post oauth_applications_path(:oauth2_application => {
 
 144                                      :name => "Test Application",
 
 145                                      :redirect_uri => "https://test.example.com/",
 
 146                                      :scopes => ["read_email"]
 
 149     assert_redirected_to oauth_application_path(:id => Doorkeeper::Application.find_by(:name => "Test Application").id)
 
 154     client = create(:oauth_application, :owner => user)
 
 155     other_client = create(:oauth_application)
 
 157     get oauth_application_path(:id => client)
 
 158     assert_redirected_to login_path(:referer => oauth_application_path(:id => client.id))
 
 162     get oauth_application_path(:id => other_client)
 
 163     assert_response :not_found
 
 164     assert_template "oauth2_applications/not_found"
 
 166     get oauth_application_path(:id => client)
 
 167     assert_response :success
 
 168     assert_template "oauth2_applications/show"
 
 173     client = create(:oauth_application, :owner => user)
 
 174     other_client = create(:oauth_application)
 
 176     get edit_oauth_application_path(:id => client)
 
 177     assert_redirected_to login_path(:referer => edit_oauth_application_path(:id => client.id))
 
 181     get edit_oauth_application_path(:id => other_client)
 
 182     assert_response :not_found
 
 183     assert_template "oauth2_applications/not_found"
 
 185     get edit_oauth_application_path(:id => client)
 
 186     assert_response :success
 
 187     assert_template "oauth2_applications/edit"
 
 188     assert_select "#content form", 1 do
 
 189       assert_select "input#oauth2_application_name", 1
 
 190       assert_select "textarea#oauth2_application_redirect_uri", 1
 
 191       assert_select "input#oauth2_application_confidential", 1
 
 192       Oauth.scopes.each do |scope|
 
 193         assert_select "input#oauth2_application_scopes_#{scope.name}", 1
 
 200     client = create(:oauth_application, :owner => user)
 
 201     other_client = create(:oauth_application)
 
 203     put oauth_application_path(:id => client)
 
 204     assert_response :forbidden
 
 208     put oauth_application_path(:id => other_client)
 
 209     assert_response :not_found
 
 210     assert_template "oauth2_applications/not_found"
 
 212     put oauth_application_path(:id => client,
 
 213                                :oauth2_application => {
 
 217     assert_response :success
 
 218     assert_template "oauth2_applications/edit"
 
 220     put oauth_application_path(:id => client,
 
 221                                :oauth2_application => {
 
 223                                  :redirect_uri => "https://new.example.com/url"
 
 225     assert_redirected_to oauth_application_path(:id => client.id)
 
 230     client = create(:oauth_application, :owner => user)
 
 231     other_client = create(:oauth_application)
 
 233     assert_difference "Doorkeeper::Application.count", 0 do
 
 234       delete oauth_application_path(:id => client)
 
 236     assert_response :forbidden
 
 240     assert_difference "Doorkeeper::Application.count", 0 do
 
 241       delete oauth_application_path(:id => other_client)
 
 243     assert_response :not_found
 
 244     assert_template "oauth2_applications/not_found"
 
 246     assert_difference "Doorkeeper::Application.count", -1 do
 
 247       delete oauth_application_path(:id => client)
 
 249     assert_redirected_to oauth_applications_path