-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f0725c
commit b663b57
Showing
8 changed files
with
37 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec| | |
spec.metadata = { "rubygems_mfa_required" => "true" } | ||
spec.name = "decidim-apiauth" | ||
spec.version = Decidim::Apiauth::VERSION | ||
spec.required_ruby_version = ">= 3.0" | ||
spec.required_ruby_version = ">= 3.1" | ||
spec.authors = ["Antti Hukkanen"] | ||
spec.email = ["[email protected]"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,25 +5,25 @@ | |
|
||
module Decidim | ||
module Apiauth | ||
describe SessionsController, type: :controller do | ||
describe SessionsController do | ||
routes { Decidim::Apiauth::Engine.routes } | ||
|
||
let(:organization) { create(:organization) } | ||
let(:email) { "[email protected]" } | ||
let(:password) { "decidim123456789" } | ||
let!(:user) { create(:user, :confirmed, :admin, organization: organization, email: email, password: password) } | ||
let!(:user) { create(:user, :confirmed, :admin, organization:, email:, password:) } | ||
let(:params) do | ||
{ | ||
user: { | ||
email: email, | ||
password: password | ||
email:, | ||
password: | ||
} | ||
} | ||
end | ||
let(:invalid_params) do | ||
{ | ||
user: { | ||
email: email, | ||
email:, | ||
password: "maga2020" | ||
} | ||
} | ||
|
@@ -38,10 +38,10 @@ module Apiauth | |
describe "sign in" do | ||
it "returns jwt_token when credentials are valid" do | ||
expect(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]).not_to be_present | ||
post :create, params: params | ||
post(:create, params:) | ||
expect(response).to have_http_status(:ok) | ||
expect(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]).to be_present | ||
parsed_response_body = JSON.parse(response.body) | ||
parsed_response_body = response.parsed_body | ||
expect(parsed_response_body["jwt_token"]).to eq(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]) | ||
end | ||
|
||
|
@@ -53,9 +53,9 @@ module Apiauth | |
|
||
it "renders resource witout jwt_token in body when Tokendispatcher::ENV_KEY is nil" do | ||
@request.env[::Warden::JWTAuth::Middleware::TokenDispatcher::ENV_KEY] = nil | ||
post :create, params: params | ||
post(:create, params:) | ||
expect(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]).to be_present | ||
parsed_response_body = JSON.parse(response.body) | ||
parsed_response_body = response.parsed_body | ||
expect(parsed_response_body.has_key?("jwt_token")).to be(false) | ||
end | ||
|
||
|
@@ -66,10 +66,10 @@ module Apiauth | |
|
||
it "returns jwt_token when credentials are valid" do | ||
expect(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]).not_to be_present | ||
post :create, params: params | ||
post(:create, params:) | ||
expect(response).to have_http_status(:ok) | ||
expect(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]).to be_present | ||
parsed_response_body = JSON.parse(response.body) | ||
parsed_response_body = response.parsed_body | ||
expect(parsed_response_body["jwt_token"]).to eq(request.env[::Warden::JWTAuth::Hooks::PREPARED_TOKEN_ENV_KEY]) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,19 @@ | |
|
||
require "spec_helper" | ||
|
||
RSpec.describe "Api authentication", type: :request do | ||
RSpec.describe "ApiAuthentication" do | ||
let(:sign_in_path) { "/api/sign_in" } | ||
let(:sign_out_path) { "/api/sign_out" } | ||
|
||
let(:organization) { create(:organization) } | ||
let(:email) { "[email protected]" } | ||
let(:password) { "decidim123456789" } | ||
let!(:user) { create(:user, :confirmed, :admin, organization: organization, email: email, password: password) } | ||
let!(:user) { create(:user, :confirmed, :admin, organization:, email:, password:) } | ||
let(:params) do | ||
{ | ||
user: { | ||
email: email, | ||
password: password | ||
email:, | ||
password: | ||
} | ||
} | ||
end | ||
|
@@ -34,22 +34,22 @@ | |
end | ||
|
||
it "signs in" do | ||
post sign_in_path, params: params | ||
post(sign_in_path, params:) | ||
expect(response.headers["Authorization"]).to be_present | ||
expect(response.body["jwt_token"]).to be_present | ||
parsed_response_body = JSON.parse(response.body) | ||
parsed_response_body = response.parsed_body | ||
expect(response.headers["Authorization"].split[1]).to eq(parsed_response_body["jwt_token"]) | ||
end | ||
|
||
it "renders resource when invalid credentials" do | ||
post sign_in_path, params: invalid_params | ||
parsed_response_body = JSON.parse(response.body) | ||
parsed_response_body = response.parsed_body | ||
expect(parsed_response_body["email"]).to eq(hacker_email) | ||
expect(parsed_response_body["jwt_token"]).not_to be_present | ||
end | ||
|
||
it "signs out" do | ||
post sign_in_path, params: params | ||
post(sign_in_path, params:) | ||
expect(response).to have_http_status(:ok) | ||
authorzation = response.headers["Authorization"] | ||
orginal_count = Decidim::Apiauth::JwtBlacklist.count | ||
|
@@ -59,13 +59,13 @@ | |
|
||
context "when signed in" do | ||
before do | ||
post sign_in_path, params: params | ||
post sign_in_path, params: | ||
end | ||
|
||
it "can use token to post to api" do | ||
authorzation = response.headers["Authorization"] | ||
post "/api", params: { query: query }, headers: { HTTP_AUTHORIZATION: authorzation } | ||
parsed_response = JSON.parse(response.body)["data"] | ||
post "/api", params: { query: }, headers: { HTTP_AUTHORIZATION: authorzation } | ||
parsed_response = response.parsed_body["data"] | ||
expect(parsed_response["session"]["user"]["id"].to_i).to eq(user.id) | ||
expect(parsed_response["session"]["user"]["nickname"]).to eq(user.nickname.prepend("@")) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters