Skip to content
Extraits de code Groupes Projets
protected_resource_endpoint_spec.rb 2,42 ko
Newer Older
  • Learn to ignore specific revisions
  • Augier's avatar
    Augier a validé
    require "spec_helper"
    
    theworldbright's avatar
    theworldbright a validé
    describe Api::OpenidConnect::ProtectedResourceEndpoint, type: :request do
    
      # TODO: Replace with factory
    
    theworldbright's avatar
    theworldbright a validé
      let!(:client) do
    
    theworldbright's avatar
    theworldbright a validé
        Api::OpenidConnect::OAuthApplication.create!(
    
    theworldbright's avatar
    theworldbright a validé
          client_name: "Diaspora Test Client", redirect_uris: ["http://localhost:3000/"])
      end
      let(:auth_with_read) do
    
    theworldbright's avatar
    theworldbright a validé
        auth = Api::OpenidConnect::Authorization.create!(o_auth_application: client, user: alice)
    
    theworldbright's avatar
    theworldbright a validé
        auth.scopes << [Api::OpenidConnect::Scope.find_by!(name: "openid"),
                        Api::OpenidConnect::Scope.find_by!(name: "read")]
    
    theworldbright's avatar
    theworldbright a validé
        auth
      end
      let!(:access_token_with_read) { auth_with_read.create_access_token.to_s }
      let(:invalid_token) { SecureRandom.hex(32).to_s }
    
      # TODO: Add tests for expired access tokens
    
    
      context "when valid access token is provided" do
        before do
    
          get api_openid_connect_user_info_path, access_token: access_token_with_read
    
        it "includes private in the cache-control header" do
          expect(response.headers["Cache-Control"]).to include("private")
    
    theworldbright's avatar
    theworldbright a validé
      end
    
    theworldbright's avatar
    theworldbright a validé
      context "when no access token is provided" do
    
    theworldbright's avatar
    theworldbright a validé
        before do
    
          get api_openid_connect_user_info_path
    
    theworldbright's avatar
    theworldbright a validé
        end
    
        it "should respond with a 401 Unauthorized response" do
    
    theworldbright's avatar
    theworldbright a validé
          expect(response.status).to be(401)
    
    theworldbright's avatar
    theworldbright a validé
        it "should have an auth-scheme value of Bearer" do
          expect(response.headers["WWW-Authenticate"]).to include("Bearer")
        end
      end
    
    theworldbright's avatar
    theworldbright a validé
      context "when an invalid access token is provided" do
        before do
    
          get api_openid_connect_user_info_path, access_token: invalid_token
    
    theworldbright's avatar
    theworldbright a validé
        end
    
    theworldbright's avatar
    theworldbright a validé
        it "should respond with a 401 Unauthorized response" do
          expect(response.status).to be(401)
        end
    
    theworldbright's avatar
    theworldbright a validé
        it "should have an auth-scheme value of Bearer" do
          expect(response.headers["WWW-Authenticate"]).to include("Bearer")
        end
    
    theworldbright's avatar
    theworldbright a validé
        it "should contain an invalid_token error" do
          expect(response.body).to include("invalid_token")
    
    theworldbright's avatar
    theworldbright a validé
      end
    
    theworldbright's avatar
    theworldbright a validé
      context "when authorization has been destroyed" do
        before do
          auth_with_read.destroy
    
          get api_openid_connect_user_info_path, access_token: access_token_with_read
    
    theworldbright's avatar
    theworldbright a validé
        end
    
    theworldbright's avatar
    theworldbright a validé
        it "should respond with a 401 Unauthorized response" do
          expect(response.status).to be(401)
        end
    
    theworldbright's avatar
    theworldbright a validé
        it "should have an auth-scheme value of Bearer" do
          expect(response.headers["WWW-Authenticate"]).to include("Bearer")
        end
    
    theworldbright's avatar
    theworldbright a validé
        it "should contain an invalid_token error" do
          expect(response.body).to include("invalid_token")