Skip to content
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

Refractor: Tomato to sofia #469

Draft
wants to merge 3 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
# Technical fields
validates :login_enabled, inclusion: [true, false]
validate :password_when_activated?
validate :allow_tomato_sharing_valid?
validate :allow_sofia_sharing_valid?

# Preferences
validates :almanak_subscription_preference, presence: true, inclusion: {
Expand Down Expand Up @@ -82,7 +82,7 @@

scope :activated, (-> { where('activated_at < ?', Time.zone.now) })
scope :contactsync_users, (-> { where.not(webdav_secret_key: nil) })
scope :tomato_users, (-> { where(allow_tomato_sharing: true) })
scope :sofia_users, (-> { where(allow_sofia_sharing: true) })
scope :login_enabled, (-> { where(login_enabled: true) })
scope :sidekiq_access, (-> { where(sidekiq_access: true) })
scope :birthday, (lambda { |month = Time.zone.now.month, day = Time.zone.now.day|
Expand Down Expand Up @@ -211,10 +211,10 @@
password_digest.blank?
end

def allow_tomato_sharing_valid?
return unless allow_tomato_sharing_changed?(from: true, to: false)
def allow_sofia_sharing_valid?
return unless allow_sofia_sharing_changed?(from: true, to: false)

errors.add(:allow_tomato_sharing,
errors.add(:allow_sofia_sharing,

Check warning on line 217 in app/models/user.rb

View check run for this annotation

Codecov / codecov/patch

app/models/user.rb#L217

Added line #L217 was not covered by tests
'before being removed from sofia your credits needs to be zero.
Please ask the board to be removed from sofia.')
end
Expand Down
8 changes: 4 additions & 4 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
def resolve
if user
scope
elsif tomato?
scope.tomato_users
elsif sofia?
scope.sofia_users

Check warning on line 7 in app/policies/user_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/user_policy.rb#L6-L7

Added lines #L6 - L7 were not covered by tests
end
end

def tomato?
@application&.scopes&.include? 'tomato'
def sofia?
@application&.scopes&.include?('tomato') || @application.scopes.include?('sofia')

Check warning on line 12 in app/policies/user_policy.rb

View check run for this annotation

Codecov / codecov/patch

app/policies/user_policy.rb#L12

Added line #L12 was not covered by tests
end
end

Expand Down
14 changes: 7 additions & 7 deletions app/resources/v1/user_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:phone_number, :food_preferences, :vegetarian, :study, :start_study,
:picture_publication_preference, :ical_secret_key, :webdav_secret_key,
:password, :avatar, :avatar_url, :avatar_thumb_url,
:user_details_sharing_preference, :allow_tomato_sharing
:user_details_sharing_preference, :allow_sofia_sharing

def avatar_url
@model.avatar.url
Expand Down Expand Up @@ -57,14 +57,14 @@
allowed_keys += %i[login_enabled otp_required activated_at emergency_contact
emergency_number ifes_data_sharing_preference info_in_almanak
almanak_subscription_preference digtus_subscription_preference
user_details_sharing_preference allow_tomato_sharing]
user_details_sharing_preference allow_sofia_sharing]
end
allowed_keys += %i[picture_publication_preference] if read_or_me?
if read_user_details? && !application_is_tomato?
if read_user_details? && !application_is_sofia?

Check warning on line 63 in app/resources/v1/user_resource.rb

View check run for this annotation

Codecov / codecov/patch

app/resources/v1/user_resource.rb#L63

Added line #L63 was not covered by tests
allowed_keys += %i[email birthday address postcode city phone_number food_preferences vegetarian
study start_study]
end
allowed_keys += %i[email birthday] if application_is_tomato? && @model.allow_tomato_sharing
allowed_keys += %i[email birthday] if application_is_sofia? && @model.allow_sofia_sharing

Check warning on line 67 in app/resources/v1/user_resource.rb

View check run for this annotation

Codecov / codecov/patch

app/resources/v1/user_resource.rb#L67

Added line #L67 was not covered by tests
super && allowed_keys
end
# rubocop:enable all
Expand All @@ -76,7 +76,7 @@
emergency_contact emergency_number]
if me?(context)
attributes += %i[otp_required password
user_details_sharing_preference allow_tomato_sharing
user_details_sharing_preference allow_sofia_sharing
picture_publication_preference info_in_almanak
ifes_data_sharing_preference]
end
Expand Down Expand Up @@ -141,10 +141,10 @@
me? || update_or_me?)
end

def application_is_tomato?
def application_is_sofia?
return false unless context.key?(:application) && context.fetch(:application)

context.fetch(:application).scopes.to_a.include?('tomato')
context.fetch(:application).scopes.to_a.include?('tomato') || context.fetch(:application).scopes.to_a.include?('sofia')

Check failure on line 147 in app/resources/v1/user_resource.rb

View workflow job for this annotation

GitHub Actions / Lint

Layout/LineLength: Line is too long. [123/100] (https://rubystyle.guide#max-line-length)

Check warning on line 147 in app/resources/v1/user_resource.rb

View check run for this annotation

Codecov / codecov/patch

app/resources/v1/user_resource.rb#L147

Added line #L147 was not covered by tests
end

def update_or_me?
Expand Down
11 changes: 6 additions & 5 deletions config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
hash_token_secrets
hash_application_secrets

optional_scopes :tomato
optional_scopes :tomato, :sofia

# See https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Resource-Owner-Password-Credentials-flow
grant_flows %w[password authorization_code client_credentials]
Expand Down Expand Up @@ -42,12 +42,13 @@
after_successful_authorization do |_, auth|
# To SOFIA, a CodeResponse is returned
if auth.auth.is_a?(Doorkeeper::OAuth::CodeResponse)
# We are only interested authorization for the tomato scope
is_tomato = auth.auth.pre_auth.scopes.include?('tomato')
# We are only interested authorization for the tomato or sofia scope
# Check if either 'tomato' or 'sofia' is included in the scopes
is_sofia = auth.auth.pre_auth.scopes.include?('tomato') || auth.auth.pre_auth.scopes.include?('sofia')

Check failure on line 47 in config/initializers/doorkeeper.rb

View workflow job for this annotation

GitHub Actions / Lint

Layout/LineLength: Line is too long. [108/100] (https://rubystyle.guide#max-line-length)
user = auth.auth.pre_auth.resource_owner

if is_tomato && !user.allow_tomato_sharing
user.allow_tomato_sharing = true
if is_sofia && !user.allow_sofia_sharing
user.allow_sofia_sharing = true
user.save!
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,19 @@
end
end

context 'when allow_tomato_sharing is changed' do
context 'when allow_sofia_sharing is changed' do
context 'from nil to false' do
let(:user) { create(:user, allow_tomato_sharing: nil) }
let(:user) { create(:user, allow_sofia_sharing: nil) }

before { user.allow_tomato_sharing = false }
before { user.allow_sofia_sharing = false }

it { expect(user).to be_valid }
end

context 'from true to false' do
let(:user) { create(:user, allow_tomato_sharing: true) }
let(:user) { create(:user, allow_sofia_sharing: true) }

before { user.allow_tomato_sharing = false }
before { user.allow_sofia_sharing = false }

it { expect(user).not_to be_valid }
end
Expand Down Expand Up @@ -333,10 +333,10 @@
end
end

describe '.tomato_users' do
describe '.sofia_users' do
it do
expect { create(:user, allow_tomato_sharing: true) }.to(
change { described_class.tomato_users.count }.by(1)
expect { create(:user, allow_sofia_sharing: true) }.to(
change { described_class.sofia_users.count }.by(1)
)
end
end
Expand Down
21 changes: 17 additions & 4 deletions spec/policies/user_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@
it { expect(policy).to permit(user, create(:user)) }
end

describe 'when with tomato application' do
let(:application) { Doorkeeper::Application.create(name: 'Tomato', scopes: 'tomato') }
describe 'when with sofia application' do
let(:application) { Doorkeeper::Application.create(name: 'Sofia', scopes: 'sofia') }

it do
expect(policy).to permit(application,
create(:user, allow_tomato_sharing: true))
create(:user, allow_sofia_sharing: true))
end

it do
expect(policy).not_to permit(application,
create(:user, allow_tomato_sharing: false))
create(:user, allow_sofia_sharing: false))
end
end

Check failure on line 31 in spec/policies/user_policy_spec.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] RSpec/EmptyLineAfterExampleGroup: Add an empty line after describe. (https://rspec.rubystyle.guide/#empty-lines-between-describes, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterExampleGroup)
describe 'when with old sofia application' do

Check failure on line 32 in spec/policies/user_policy_spec.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Layout/IndentationConsistency: Inconsistent indentation detected. (https://rubystyle.guide#spaces-indentation, https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions)
let(:application) { Doorkeeper::Application.create(name: 'Old Sofia', scopes: 'tomato') }

Check failure on line 33 in spec/policies/user_policy_spec.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

Check failure on line 34 in spec/policies/user_policy_spec.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Layout/TrailingWhitespace: Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)
it do
expect(policy).to permit(application,
create(:user, allow_sofia_sharing: true))
end

Check failure on line 39 in spec/policies/user_policy_spec.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Layout/TrailingWhitespace: Trailing whitespace detected. (https://rubystyle.guide#no-trailing-whitespace)
it do
expect(policy).not_to permit(application,
create(:user, allow_sofia_sharing: false))
end
end

Check failure on line 44 in spec/policies/user_policy_spec.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Layout/BlockAlignment: end at 44, 4 is not aligned with describe 'when with old sofia application' do at 32, 6.
end

permissions :update? do
Expand Down
14 changes: 7 additions & 7 deletions spec/resources/v1/user_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
%i[login_enabled otp_required activated_at emergency_contact
emergency_number ifes_data_sharing_preference info_in_almanak
almanak_subscription_preference digtus_subscription_preference
user_details_sharing_preference allow_tomato_sharing]
user_details_sharing_preference allow_sofia_sharing]
end
let(:read_fields) do
%i[picture_publication_preference]
Expand All @@ -24,7 +24,7 @@
%i[email birthday address postcode city phone_number food_preferences vegetarian
study start_study]
end
let(:tomato_fields) do
let(:sofia_fields) do
%i[email birthday]
end
let(:another_user) { create(:user, user_details_sharing_preference: 'hidden') }
Expand Down Expand Up @@ -110,17 +110,17 @@

it { expect(resource.fetchable_fields).to match_array(basic_fields) }

context 'when with tomato scope' do
let(:application) { create(:application, scopes: 'public tomato') }
context 'when with sofia scope' do
let(:application) { create(:application, scopes: 'public sofia') }

context 'when without allowance' do
it { expect(resource.fetchable_fields).to match_array(basic_fields) }
end

context 'when with allowance' do
let(:another_user) { create(:user, allow_tomato_sharing: true) }
let(:another_user) { create(:user, allow_sofia_sharing: true) }

it { expect(resource.fetchable_fields).to match_array(basic_fields + tomato_fields) }
it { expect(resource.fetchable_fields).to match_array(basic_fields + sofia_fields) }
end
end
end
Expand All @@ -141,7 +141,7 @@
user_permissions login_enabled]
end
let(:current_user_fields) do
%i[otp_required password user_details_sharing_preference allow_tomato_sharing
%i[otp_required password user_details_sharing_preference allow_sofia_sharing
info_in_almanak ifes_data_sharing_preference picture_publication_preference]
end

Expand Down
Loading