Skip to content

Commit f08d5c1

Browse files
committed
Rewrite authorization mechanism: more flexibility
1 parent ed3e72f commit f08d5c1

28 files changed

+223
-121
lines changed

spec/features/media_stores_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
end
1717

1818
it_displays "authentication error"
19-
it_displays "authorization error"
2019
end
2120

2221
context "for an ordinary user" do
@@ -63,13 +62,13 @@
6362
end
6463

6564
specify "page has links to users" do
66-
expect(page).to have_link("0", href: "/media-service/stores/legacy-file-store/users/")
67-
expect(page).to have_link("0", href: "/media-service/stores/database/users/")
65+
expect(page).to have_link("0", href: "/media-service/stores/legacy-file-store/users/")
66+
expect(page).to have_link("0", href: "/media-service/stores/database/users/")
6867
end
6968

7069
specify "page has links to groups" do
71-
expect(page).to have_link("0", href: "/media-service/stores/legacy-file-store/groups/")
72-
expect(page).to have_link("0", href: "/media-service/stores/database/groups/")
70+
expect(page).to have_link("0", href: "/media-service/stores/legacy-file-store/groups/")
71+
expect(page).to have_link("0", href: "/media-service/stores/database/groups/")
7372
end
7473
end
7574
end

spec/features/settings_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
end
2222

2323
it_displays "authentication error"
24-
it_displays "authorization error"
2524
end
2625

2726
context "for an ordinary user" do

spec/features/shared/authorization_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
visit path
88
within(".modal") do
99
expect(page).to have_css(".modal-header", text: "Request ERROR 403")
10-
expect(page).to have_css(".modal-body", text: "System-admin scope required")
10+
expect(page.find(".modal-body")).to have_content("system-admin")
1111
end
1212
end
1313
end

spec/features/stores/groups_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
end
1616

1717
it_displays "authentication error"
18-
it_displays "authorization error"
1918
end
2019

2120
context "for an ordinary user" do

spec/features/stores/users_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
end
1616

1717
it_displays "authentication error"
18-
it_displays "authorization error"
1918
end
2019

2120
context "for an ordinary user" do

spec/features/uploads_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
it "displays modal with authentication error" do
1515
visit path
1616
within(".modal") do
17-
expect(page).to have_css(".modal-header", text: "Request ERROR 403")
18-
expect(page).to have_css(".modal-body", text: "Sign-in required")
17+
expect(page).to have_css(".modal-header", text: "Request ERROR 401")
1918
end
2019
end
2120

spec/requests/authorization_spec.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe "Authorization" do
1+
describe "Authorization to settings" do
22
context "with token" do
33
let!(:settings) { create(:media_service_setting) }
44
let(:request) { faraday_client_with_token.get("settings/") }
@@ -51,7 +51,7 @@
5151
end
5252

5353
it "responds with error message" do
54-
expect(response.body).to include("System-admin scope required")
54+
expect(response.body).to include("")
5555
end
5656
end
5757

@@ -62,11 +62,8 @@
6262
expect(response.status).to eq(403)
6363
end
6464

65-
it "responds with error message" do
66-
expect(response.body).to include("System-admin scope required")
67-
end
6865
end
69-
66+
7067
context "for an user with system admin role" do
7168
let(:user) { create(:user, :with_system_admin_role) }
7269

spec/requests/settings_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'requests/shared/authentication_error'
12
require 'requests/shared/system_admin_error'
23

34
describe "Resources" do
@@ -26,7 +27,7 @@
2627

2728
# it_raises "authorization error"
2829
# include_examples "system admin access only"
29-
it_raises "system admin error"
30+
it_raises "authentication error"
3031
end
3132

3233
context "for an ordinary user" do
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
RSpec.configure do |c|
2+
c.alias_it_should_behave_like_to :it_raises
3+
end
4+
5+
shared_examples "authentication error" do
6+
let(:api_token) do
7+
create(:api_token, user: user, scope_write: true) if user
8+
end
9+
let(:user_token) { api_token&.token_hash }
10+
11+
it "responds with 401 Forbidden status" do
12+
expect(response.status).to eq(401)
13+
end
14+
15+
it "responds with error message" do
16+
expect(response.body).to include("Unauthorized - Authentication/Sign-in required")
17+
end
18+
end

spec/requests/shared/authorization_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
end
1414

1515
it "responds with error message" do
16-
expect(response.body).to include("Sign-in required")
16+
expect(response.body).to include("Authorization requirements not satisfied")
1717
end
1818
end

0 commit comments

Comments
 (0)