Skip to content
Extraits de code Groupes Projets
protected_resource_endpoint_spec.rb 1,76 ko
Newer Older
Augier's avatar
Augier a validé
require "spec_helper"
theworldbright's avatar
theworldbright a validé
describe OpenidConnect::ProtectedResourceEndpoint, type: :request do
  describe "getting the user info" do
    let!(:token) { bob.tokens.create!.bearer_token.to_s }
    let(:invalid_token) { SecureRandom.hex(32).to_s }
theworldbright's avatar
theworldbright a validé
    # TODO: Add tests for expired access tokens
theworldbright's avatar
theworldbright a validé
    context "when access token is valid" do
      it "shows the user's username and email" do
Augier's avatar
Augier a validé
        get "/api/v0/user/", access_token: token
        json_body = JSON.parse(response.body)
        expect(json_body["username"]).to eq(bob.username)
        expect(json_body["email"]).to eq(bob.email)
      it "should include private in the cache-control header" do
Augier's avatar
Augier a validé
        get "/api/v0/user/", access_token: token
        expect(response.headers["Cache-Control"]).to include("private")
      end
    end

    context "when no access token is provided" do
      it "should respond with a 401 Unauthorized response" do
        get "/api/v0/user/"
        expect(response.status).to be(401)
      end
      it "should have an auth-scheme value of Bearer" do
        get "/api/v0/user/"
        expect(response.headers["WWW-Authenticate"]).to include("Bearer")
      end
    end

    context "when an invalid access token is provided" do
      it "should respond with a 401 Unauthorized response" do
Augier's avatar
Augier a validé
        get "/api/v0/user/", access_token: invalid_token
        expect(response.status).to be(401)
      end
      it "should have an auth-scheme value of Bearer" do
Augier's avatar
Augier a validé
        get "/api/v0/user/", access_token: invalid_token
        expect(response.headers["WWW-Authenticate"]).to include("Bearer")
      end
      it "should contain an invalid_token error" do
Augier's avatar
Augier a validé
        get "/api/v0/user/", access_token: invalid_token
        expect(response.body).to include("invalid_token")
      end