Skip to content
Extraits de code Groupes Projets
token_endpoint_spec.rb 3,35 ko
Newer Older
Augier's avatar
Augier a validé
require "spec_helper"
describe OpenidConnect::TokenEndpoint, type: :request do
theworldbright's avatar
theworldbright a validé
  let!(:client) { OAuthApplication.create!(redirect_uris: ["http://localhost"]) }
  describe "the password grant type" do
    context "when the username field is missing" do
      it "should return an invalid request error" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", password: "bluepin7",
             client_id: client.client_id, client_secret: client.client_secret
        expect(response.body).to include("'username' required")
      end
    end
    context "when the password field is missing" do
      it "should return an invalid request error" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", username: "bob",
             client_id: client.client_id, client_secret: client.client_secret
        expect(response.body).to include("'password' required")
      end
    end
    context "when the username does not match an existing user" do
      it "should return an invalid request error" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", username: "randomnoexist",
             password: "bluepin7", client_id: client.client_id, client_secret: client.client_secret
        expect(response.body).to include("invalid_grant")
      end
    end
    context "when the password is invalid" do
      it "should return an invalid request error" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", username: "bob",
             password: "wrongpassword", client_id: client.client_id, client_secret: client.client_secret
        expect(response.body).to include("invalid_grant")
      end
    end
    context "when the request is valid" do
      it "should return an access token" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", username: "bob",
             password: "bluepin7", client_id: client.client_id, client_secret: client.client_secret
        json = JSON.parse(response.body)
        expect(json["access_token"].length).to eq(64)
        expect(json["token_type"]).to eq("bearer")
        expect(json.keys).to include("expires_in")
      end
    end
theworldbright's avatar
theworldbright a validé
    context "when there are duplicate fields" do
      it "should return an invalid request error" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", username: "bob", password: "bluepin7",
             username: "bob", password: "bluepin6", client_id: client.client_id, client_secret: client.client_secret
theworldbright's avatar
theworldbright a validé
        expect(response.body).to include("invalid_grant")
      end
    end
    context "when the client is unregistered" do
      it "should return an error" do
Augier's avatar
Augier a validé
        post "/openid_connect/access_tokens", grant_type: "password", username: "bob",
             password: "bluepin7", client_id: SecureRandom.hex(16).to_s, client_secret: client.client_secret
theworldbright's avatar
theworldbright a validé
        expect(response.body).to include("invalid_client")
      end
    end
Augier's avatar
Augier a validé
    # TODO: Support a way to prevent brute force attacks using rate-limitation
    # as specified by RFC 6749 4.3.2 Access Token Request
theworldbright's avatar
theworldbright a validé
  describe "an unsupported grant type" do
    it "should return an unsupported grant type error" do
Augier's avatar
Augier a validé
      post "/openid_connect/access_tokens", grant_type: "noexistgrant", username: "bob",
           password: "bluepin7", client_id: client.client_id, client_secret: client.client_secret
      expect(response.body).to include "unsupported_grant_type"
    end
  end
end