Repository navigation
feat: add an authenticated preference version endpoint #666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| class PreferenceVersionsController < ApiController | ||
| def show | ||
| response.set_header("Cache-Control", "private, no-store") | ||
| render json: { version: PreferenceVersion.for(current_user) } | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "digest" | ||
|
|
||
| # Fingerprints preference inputs without resolving each meeting's templates. | ||
| # Include record identities and values so deletions and bulk writes also change | ||
| # the token; unrelated users and display-only settings cannot invalidate it. | ||
| class PreferenceVersion | ||
| def self.for(user) | ||
| fields = PreferenceResolver::PREFERENCE_FIELDS | ||
| inputs = { | ||
| user: user.id, | ||
| events: user.event_preferences.order(:id).pluck(:id, :preferenceable_type, :preferenceable_id, *fields), | ||
| calendars: user.calendar_preferences.order(:id).pluck(:id, :scope, :event_type, *fields), | ||
| colors: UserExtensionConfig.where(user: user).pick(:default_color_lecture, :default_color_lab), | ||
| notifications_disabled: user.notifications_disabled?, | ||
|
Cattn marked this conversation as resolved.
|
||
| defaults: PreferenceResolver.defaults | ||
| } | ||
|
|
||
| Digest::SHA256.hexdigest(JSON.generate(inputs)) | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe "GET /api/user/preferences/version", type: :request do | ||
| let(:user) { create(:user) } | ||
| let(:headers) { auth_headers_for(user) } | ||
|
|
||
| def json = JSON.parse(response.body) | ||
|
|
||
| it "returns only the signed-in user's opaque token and prohibits response caching" do | ||
| create(:event_preference, user: user, title_template: "Private title") | ||
| expected_version = PreferenceVersion.for(user) | ||
|
|
||
| get "/api/user/preferences/version", headers: headers | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(response.media_type).to eq("application/json") | ||
| expect(json).to eq("version" => expected_version) | ||
| expect(json["version"]).to match(/\A[0-9a-f]{64}\z/) | ||
| expect(response.headers["Cache-Control"]).to eq("private, no-store") | ||
|
|
||
| get "/api/user/preferences/version", headers: headers | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(json["version"]).to eq(expected_version) | ||
| end | ||
|
|
||
| it "reflects persisted preference changes on the next request" do | ||
| preference = create(:calendar_preference, user: user, title_template: "Original") | ||
|
|
||
| get "/api/user/preferences/version", headers: headers | ||
| expect(response).to have_http_status(:ok) | ||
| version = json.fetch("version") | ||
|
|
||
| preference.update_columns(title_template: "Changed") | ||
|
|
||
| get "/api/user/preferences/version", headers: headers | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(json.fetch("version")).not_to eq(version) | ||
| end | ||
|
|
||
| it "does not accept a different account through request parameters" do | ||
| other_user = create(:user) | ||
| create(:event_preference, user: other_user, title_template: "Other user's title") | ||
|
|
||
| get "/api/user/preferences/version", params: { user_id: other_user.id }, headers: headers | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(json).to eq("version" => PreferenceVersion.for(user)) | ||
| expect(json["version"]).not_to eq(PreferenceVersion.for(other_user)) | ||
| end | ||
|
|
||
| it "requires authentication" do | ||
| get "/api/user/preferences/version" | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| expect(json["code"]).to eq("AUTH_MISSING") | ||
| expect(json).not_to have_key("version") | ||
| end | ||
|
|
||
| it "rejects a revoked session" do | ||
| token = api_token_for(user) | ||
| UserSession.find_by!(jti: JsonWebTokenService.decode(token)[:jti]).revoke! | ||
|
|
||
| get "/api/user/preferences/version", headers: { "Authorization" => "Bearer #{token}" } | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| expect(json["code"]).to eq("AUTH_REVOKED") | ||
| expect(json).not_to have_key("version") | ||
| end | ||
|
|
||
| it "does not resolve templates or enqueue calendar sync work" do | ||
| create(:event_preference, user: user, title_template: "Saved title") | ||
| request_headers = headers | ||
| expect(PreferenceResolver).not_to receive(:new) | ||
| expect(CalendarTemplateRenderer).not_to receive(:new) | ||
| expect(GoogleCalendarSyncJob).not_to receive(:perform_later) | ||
|
|
||
| get "/api/user/preferences/version", headers: request_headers | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe PreferenceVersion do | ||
| include ActiveSupport::Testing::TimeHelpers | ||
|
|
||
| let(:user) { create(:user) } | ||
|
|
||
| before { allow(GoogleCalendarSyncJob).to receive(:perform_later) } | ||
|
|
||
| it "returns a stable opaque token for unchanged persisted inputs" do | ||
| create(:event_preference, user: user) | ||
| create(:calendar_preference, user: user) | ||
|
|
||
| version = described_class.for(user) | ||
|
|
||
| expect(version).to match(/\A[0-9a-f]{64}\z/) | ||
| expect(described_class.for(user.reload)).to eq(version) | ||
| end | ||
|
|
||
| { | ||
| title_template: "Saved title", | ||
| description_template: "Saved description", | ||
| location_template: "Saved location", | ||
| reminder_settings: [ { "time" => "45", "type" => "minutes", "method" => "popup" } ], | ||
| color_id: "#abcdef", | ||
| visibility: "private" | ||
| }.each do |field, value| | ||
| [ :event_preference, :calendar_preference ].each do |factory| | ||
| it "detects a bulk #{field} edit to a #{factory} without a timestamp change" do | ||
| preference = create(factory, user: user) | ||
| version = described_class.for(user) | ||
| timestamp = preference.updated_at | ||
|
|
||
| preference.class.where(id: preference.id).update_all(field => value) | ||
|
|
||
| expect(preference.reload.updated_at).to eq(timestamp) | ||
| expect(described_class.for(user)).not_to eq(version) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| [ :event_preference, :calendar_preference ].each do |factory| | ||
| it "detects creation and deletion of a #{factory}" do | ||
| empty_version = described_class.for(user) | ||
| preference = create(factory, user: user) | ||
| populated_version = described_class.for(user) | ||
|
|
||
| expect(populated_version).not_to eq(empty_version) | ||
|
|
||
| preference.class.where(id: preference.id).delete_all | ||
|
|
||
| expect(described_class.for(user)).to eq(empty_version) | ||
| end | ||
|
|
||
| it "detects nested reminder changes in a #{factory}" do | ||
| preference = create(factory, user: user, | ||
| reminder_settings: [ { "time" => "30", "type" => "minutes", "method" => "popup" } ]) | ||
| version = described_class.for(user) | ||
|
|
||
| preference.update!(reminder_settings: [ { "time" => "30", "type" => "minutes", "method" => "email" } ]) | ||
|
|
||
| expect(described_class.for(user)).not_to eq(version) | ||
| end | ||
|
|
||
| it "distinguishes inherited reminders from disabled reminders in a #{factory}" do | ||
| preference = create(factory, user: user, title_template: "Saved title", reminder_settings: nil) | ||
| version = described_class.for(user) | ||
|
|
||
| preference.update!(reminder_settings: []) | ||
|
|
||
| expect(described_class.for(user)).not_to eq(version) | ||
| end | ||
|
|
||
| it "ignores timestamp-only changes to a #{factory}" do | ||
| preference = create(factory, user: user) | ||
| version = described_class.for(user) | ||
|
|
||
| preference.update_columns(updated_at: preference.updated_at + 1.hour) | ||
|
|
||
| expect(described_class.for(user)).to eq(version) | ||
| end | ||
| end | ||
|
|
||
| [ :default_color_lecture, :default_color_lab ].each do |field| | ||
| it "detects a change to #{field}" do | ||
| version = described_class.for(user) | ||
|
|
||
| user.user_extension_config.update!(field => "#abcdef") | ||
|
|
||
| expect(described_class.for(user)).not_to eq(version) | ||
| end | ||
| end | ||
|
|
||
| it "ignores display-only settings" do | ||
| config = user.user_extension_config | ||
| version = described_class.for(user) | ||
|
|
||
| config.update!(military_time: !config.military_time, | ||
| advanced_editing: !config.advanced_editing, | ||
| show_historic_terms: !config.show_historic_terms) | ||
|
|
||
| expect(described_class.for(user)).to eq(version) | ||
| end | ||
|
|
||
| it "detects notification suppression and its expiry without another write" do | ||
| travel_to(Time.zone.local(2026, 10, 7, 12, 0)) do | ||
| enabled_version = described_class.for(user) | ||
| user.update!(notifications_disabled_until: 5.minutes.from_now) | ||
| disabled_version = described_class.for(user) | ||
|
|
||
| expect(disabled_version).not_to eq(enabled_version) | ||
|
|
||
| travel 6.minutes | ||
|
|
||
| expect(described_class.for(user)).to eq(enabled_version) | ||
| end | ||
| end | ||
|
|
||
| it "detects notifications being enabled again" do | ||
| user.disable_notifications! | ||
| version = described_class.for(user) | ||
|
|
||
| user.enable_notifications! | ||
|
|
||
| expect(described_class.for(user)).not_to eq(version) | ||
| end | ||
|
|
||
| it "isolates tokens from another user's preference, color, and notification changes" do | ||
| other_user = create(:user) | ||
| version = described_class.for(user) | ||
|
|
||
| expect(described_class.for(other_user)).not_to eq(version) | ||
|
|
||
| event_preference = create(:event_preference, user: other_user, title_template: "Other title") | ||
| calendar_preference = create(:calendar_preference, user: other_user, title_template: "Other title") | ||
| other_user.user_extension_config.update!(default_color_lecture: "#abcdef", default_color_lab: "#123456") | ||
| other_user.disable_notifications! | ||
|
|
||
| expect(described_class.for(user)).to eq(version) | ||
|
|
||
| event_preference.update!(title_template: "Other edit") | ||
| calendar_preference.update!(reminder_settings: []) | ||
| event_preference.destroy! | ||
| calendar_preference.destroy! | ||
|
|
||
| expect(described_class.for(user)).to eq(version) | ||
| end | ||
|
|
||
| [ :SYSTEM_DEFAULTS, :FINAL_EXAM_DEFAULTS, :UNI_CAL_DEFAULTS, :UNI_CAL_ALL_DAY_REMINDERS, :UNI_CAL_TIMED_REMINDERS ].each do |constant| | ||
| it "detects a change to resolver #{constant}" do | ||
| version = described_class.for(user) | ||
| original = PreferenceResolver.const_get(constant) | ||
| replacement = [ { "time" => "45", "type" => "minutes", "method" => "popup" } ] | ||
| replacement = original.merge(title_template: "New default title") if original.is_a?(Hash) | ||
|
|
||
| stub_const("PreferenceResolver::#{constant}", replacement) | ||
|
|
||
| expect(described_class.for(user)).not_to eq(version) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.