Skip to content

Commit f85e284

Browse files
committed
[B] Ensure that project creators can read ingestion messages
1 parent 631b073 commit f85e284

File tree

3 files changed

+131
-13
lines changed

3 files changed

+131
-13
lines changed

api/app/controllers/api/v1/ingestions/relationships/ingestion_messages_controller.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ module V1
55
module Ingestions
66
module Relationships
77
class IngestionMessagesController < ApplicationController
8-
before_action :set_project
9-
10-
resourceful! Ingestion do
11-
@project.nil? ? Ingestion : @project.ingestions
12-
end
8+
resourceful! Ingestion, authorize_options: { except: %i[index] }
139

1410
def index
1511
@ingestion = load_ingestion
12+
13+
authorize_action_for @ingestion
14+
1615
permitted = params.permit(:starting_at)
17-
time = permitted[:starting_at] ? DateTime.parse(permitted[:starting_at]) : DateTime.new
18-
render_multiple_resources @ingestion.ingestion_messages.where(
19-
created_at: time..DateTime.now
20-
)
21-
end
2216

23-
private
17+
time = permitted[:starting_at].present? ? Time.zone.parse(permitted[:starting_at]) : nil
2418

25-
def set_project
26-
@project = Project.friendly.find(params[:project_id]) if params[:project_id]
19+
render_multiple_resources @ingestion.ingestion_messages.where(
20+
created_at: time..Time.current
21+
)
2722
end
2823
end
2924
end
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe API::V1::Ingestions::Relationships::IngestionMessagesController, type: :request do
4+
let_it_be(:project, refind: true) { FactoryBot.create(:project, creator: project_creator) }
5+
let_it_be(:ingestion, refind: true) { FactoryBot.create(:ingestion, project:) }
6+
let_it_be(:other_ingestion, refind: true) { FactoryBot.create(:ingestion) }
7+
8+
let_it_be(:ingestion_messages, refind: true) do
9+
[
10+
FactoryBot.create(:ingestion_message, :start_message, ingestion:, created_at: 2.hours.ago),
11+
FactoryBot.create(:ingestion_message, :info, ingestion:, created_at: 90.minutes.ago),
12+
FactoryBot.create(:ingestion_message, :warn, ingestion:, created_at: 70.minutes.ago),
13+
FactoryBot.create(:ingestion_message, :unknown, ingestion:, created_at: 50.minutes.ago),
14+
FactoryBot.create(:ingestion_message, :end_message, ingestion:),
15+
]
16+
end
17+
18+
let(:starting_at) { nil }
19+
let(:params) { { starting_at: } }
20+
21+
shared_examples_for "visible ingestion messages" do
22+
it "returns ingestion messages" do
23+
expect do
24+
get path, headers:
25+
end.to execute_safely
26+
27+
expect(response).to have_http_status(:ok)
28+
expect(response.parsed_body["data"]).to have(5).items
29+
end
30+
31+
context "when filtering by starting_at" do
32+
let(:starting_at) { 40.minutes.ago.iso8601 }
33+
34+
it "returns ingestion messages created after the starting_at time" do
35+
expect do
36+
get path, headers:
37+
end.to execute_safely
38+
39+
expect(response).to have_http_status(:ok)
40+
41+
expect(response.parsed_body["data"]).to have(1).item
42+
end
43+
end
44+
end
45+
46+
context "GET /api/v1/ingestions/:ingestion_id/relationships/ingestion_messages" do
47+
let(:path) { api_v1_ingestion_relationships_ingestion_messages_path(ingestion, params:) }
48+
let(:other_path) { api_v1_ingestion_relationships_ingestion_messages_path(other_ingestion, params:) }
49+
50+
context "as an admin" do
51+
let(:headers) { admin_headers }
52+
53+
it_behaves_like "visible ingestion messages"
54+
55+
context "when trying to read messages for an unaffiliated ingestion" do
56+
let(:path) { other_path }
57+
58+
it "allows access" do
59+
expect do
60+
get path, headers:
61+
end.to execute_safely
62+
63+
expect(response).to have_http_status(:ok)
64+
end
65+
end
66+
end
67+
68+
context "as the project creator" do
69+
let(:headers) { project_creator_headers }
70+
71+
it_behaves_like "visible ingestion messages"
72+
73+
context "when trying to read messages for an unaffiliated ingestion" do
74+
let(:path) { other_path }
75+
76+
it "forbids access" do
77+
expect do
78+
get path, headers:
79+
end.to execute_safely
80+
81+
expect(response).to have_http_status(:forbidden)
82+
end
83+
end
84+
end
85+
86+
context "as a marketeer" do
87+
let(:headers) { marketeer_headers }
88+
89+
it_behaves_like "visible ingestion messages"
90+
end
91+
92+
context "as a reader" do
93+
let(:headers) { reader_headers }
94+
95+
it "forbids access" do
96+
expect do
97+
get path, headers:
98+
end.to execute_safely
99+
100+
expect(response).to have_http_status(:forbidden)
101+
end
102+
end
103+
end
104+
end

api/spec/requests/projects_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@
8181
end
8282
end
8383

84+
context "when the user is a project creator" do
85+
let(:headers) { project_creator_headers }
86+
87+
it "creates the project and assigns the creator as an editor on the newly created project" do
88+
params = build_json_payload(attributes: { title: "foo" })
89+
90+
expect do
91+
post(path, headers:, params:)
92+
end.to change(Project, :count).by(1)
93+
94+
expect(response).to have_http_status(:created)
95+
96+
project_id = response.parsed_body.dig("data", "id")
97+
created_project = Project.find(project_id)
98+
99+
expect(project_creator).to have_role(:project_editor, created_project)
100+
end
101+
end
102+
84103
context "when the user is not logged in" do
85104
it "has a 401 status code" do
86105
params = build_json_payload(attributes: { title: "foo" })

0 commit comments

Comments
 (0)