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

Adding and adjusting tests for user role handling #114

Merged
merged 3 commits into from
May 29, 2023
Merged
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
4 changes: 2 additions & 2 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--require spec_helper
--require fixture_builder
--color
--require rails_helper
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Layout/LineLength:
Metrics/MethodLength:
Max: 20


# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
Metrics/ClassLength:
Max: 1500
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ group :test do
gem "diaspora_federation-json_schema", "~> 0.3.0"
gem "json-schema-rspec", "0.0.4"
gem "rspec-json_expectations", "~> 2.1"
gem "shoulda-matchers", "~> 5.1"
gem "shoulda-matchers", "~> 5.3"
gem "webmock", "3.14.0", require: false
end
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ DEPENDENCIES
rubocop-rspec (~> 2.11, >= 2.11.1)
ruby-oembed (~> 0.15.0)
sassc-rails (~> 2.1, >= 2.1.2)
shoulda-matchers (~> 5.1)
shoulda-matchers (~> 5.3)
sidekiq (~> 7.1)
sidekiq-cron (~> 1.10, >= 1.10.1)
sprockets-rails
Expand Down
2 changes: 1 addition & 1 deletion app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Role < ApplicationRecord
scope :admins, -> { where(name: "admin") }
scope :moderators, -> { where(name: %w[moderator admin]) }

def self.is_admin?(person)
def self.admin?(person)
exists?(person_id: person.id, name: "admin")
end

Expand Down
19 changes: 6 additions & 13 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ def self.all_sharing_with_person(person)
User.joins(:contacts).where(contacts: {person_id: person.id})
end

def basic_profile_present?
tag_followings.any? || profile[:image_url]
end

### Helpers ############
def self.build(opts={})
user = User.new(opts.except(:person, :id))
Expand Down Expand Up @@ -249,14 +245,14 @@ def send_reset_password_instructions
end

def strip_and_downcase_username
return unless username.present?
return if username.blank?

username.strip!
username.downcase!
end

def strip_and_downcase_email
return unless email.present?
return if email.blank?

email.strip!
email.downcase!
Expand All @@ -271,18 +267,15 @@ def sign_up
end

def admin?
# TODO: return if admin
false
Role.admin?(person)
end

def moderator?
# TODO: return if moderator
false
Role.moderator?(person)
end

def podmin?
# TODO: return if podmin
true
username == AppConfig.admins.account
end

######## Posting ########
Expand Down Expand Up @@ -336,7 +329,7 @@ def update_post_default_aspects(post_default_aspect_ids)

######### Mailer #######################
def mail(job, *args)
return unless job.present?
return if job.blank?

pref = job.to_s.gsub("Workers::Mail::", "").underscore
job.perform_later(*args) if disable_mail == false && !user_preferences.exists?(email_type: pref)
Expand Down
12 changes: 7 additions & 5 deletions app/models/user/connecting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

class User
module Connecting

# This will create a contact on the side of the sharer and the sharee.
# @param [Person] person The person to start sharing with.
# @param [Aspect] aspect The aspect to add them to.
# @return [Contact] The newly made contact for the passed in person.
def share_with(person, aspect)
return if blocks.where(person_id: person.id).exists?
return if blocks.exists?(person_id: person.id)

contact = contacts.find_or_initialize_by(person_id: person.id)
return false unless contact.valid?
return nil unless contact.valid?

needs_dispatch = !contact.receiving?
contact.receiving = true
Expand All @@ -34,6 +33,7 @@ def disconnect(contact)

if contact.person.local?
raise "FATAL: user entry is missing from the DB. Aborting" if contact.person.owner.nil?

contact.person.owner.disconnected_by(contact.user.person)
else
Diaspora::Federated::ContactRetraction.for(contact).defer_dispatch(self)
Expand All @@ -46,7 +46,9 @@ def disconnect(contact)

def disconnected_by(person)
logger.info "event=disconnected_by user=#{diaspora_handle} target=#{person.diaspora_handle}"
contact_for(person).try { |contact| disconnect_contact(contact, direction: :sharing, destroy: !contact.receiving) }
contact_for(person).try {|contact|
disconnect_contact(contact, direction: :sharing, destroy: !contact.receiving)
}
end

private
Expand All @@ -59,4 +61,4 @@ def disconnect_contact(contact, direction:, destroy:)
end
end
end
end
end
35 changes: 35 additions & 0 deletions app/models/user/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class User
module Profile
def update_profile(params)
if photo = params.delete(:photo)
photo.update(pending: false) if photo.pending
params[:image_url] = photo.url(:thumb_large)
params[:image_url_medium] = photo.url(:thumb_medium)
params[:image_url_small] = photo.url(:thumb_small)
end

params.stringify_keys!
params.slice!(*(Profile.column_names + %w[tag_string date]))
if profile.update(params)
deliver_profile_update
true
else
false
end
end

def update_profile_with_omniauth(user_info)
update_profile(profile.from_omniauth_hash(user_info))
end

def deliver_profile_update(opts={})
Diaspora::Federation::Dispatcher.defer_dispatch(self, profile, opts)
end

def basic_profile_present?
tag_followings.any? || profile[:image_url]
end
end
end
179 changes: 91 additions & 88 deletions app/models/user/querying.rb
Original file line number Diff line number Diff line change
@@ -1,112 +1,115 @@
# frozen_string_literal: true

module User::Querying
def find_visible_shareable_by_id(klass, id, opts={})
key = (opts.delete(:key) || :id)
find_visible_shareable_by_id = EvilQuery::VisibleShareableById.new(self, klass, key, id, opts)
find_visible_shareable_by_id.post!
end
class User
module Querying
def find_visible_shareable_by_id(klass, id, opts={})
key = (opts.delete(:key) || :id)
find_visible_shareable_by_id = EvilQuery::VisibleShareableById.new(self, klass, key, id, opts)
find_visible_shareable_by_id.post!
end

def visible_shareables(klass, opts={})
opts = prep_opts(klass, opts)
shareable_ids = visible_shareable_ids(klass, opts)
klass.where(id: shareable_ids).select("DISTINCT #{klass.table_name}.*")
.limit(opts[:limit]).order(opts[:order_with_table])
end
def visible_shareables(klass, opts={})
opts = prep_opts(klass, opts)
shareable_ids = visible_shareable_ids(klass, opts)
klass.where(id: shareable_ids).select("DISTINCT #{klass.table_name}.*")
.limit(opts[:limit]).order(opts[:order_with_table])
end

# @param [TrueClass] with_order
def posts_from(person, with_order: true)
base_query = Post.from_person_visible_by_user(self, person)
return base_query.order("posts.created_at desc") if with_order
# @param [TrueClass] with_order
def posts_from(person, with_order: true)
base_query = Post.from_person_visible_by_user(self, person)
return base_query.order("posts.created_at desc") if with_order

base_query
end
base_query
end

def photos_from(person, opts={})
opts = prep_opts(Photo, opts)
Photo.from_person_visible_by_user(self, person)
.limit(opts[:limit])
end
def photos_from(person, opts={})
opts = prep_opts(Photo, opts)
Photo.from_person_visible_by_user(self, person)
.limit(opts[:limit])
end

def contact_for(person)
return nil unless person
def contact_for(person)
return nil unless person

contact_for_person_id(person.id)
end
contact_for_person_id(person.id)
end

def block_for(person)
return nil unless person
blocks.find_by(person_id: person.id)
end
def block_for(person)
return nil unless person

def aspects_with_shareable(base_class_name_or_class, shareable_id)
base_class_name = base_class_name_or_class
base_class_name = base_class_name_or_class.base_class.to_s if base_class_name_or_class.is_a?(Class)
self.aspects.joins(:aspect_visibilities).where(:aspect_visibilities => {:shareable_id => shareable_id, :shareable_type => base_class_name})
end
blocks.find_by(person_id: person.id)
end

def contact_for_person_id(person_id)
Contact.includes(person: :profile)
.find_by(user_id: id, person_id: person_id)
end
def aspects_with_shareable(base_class_name_or_class, shareable_id)
base_class_name = base_class_name_or_class
base_class_name = base_class_name_or_class.base_class.to_s if base_class_name_or_class.is_a?(Class)
aspects.joins(:aspect_visibilities).where(aspect_visibilities: {shareable_id: shareable_id,
shareable_type: base_class_name})
end

def contact_for_person_id(person_id)
Contact.includes(person: :profile)
.find_by(user_id: id, person_id: person_id)
end

# @param [Person] person
# @return [Boolean] whether person is a contact of this user
def has_contact_for?(person)
Contact.exists?(:user_id => self.id, :person_id => person.id)
end
# @param [Person] person
# @return [Boolean] whether person is a contact of this user
def has_contact_for?(person)
Contact.exists?(user_id: id, person_id: person.id)
end

def people_in_aspects(requested_aspects, opts={})
allowed_aspects = self.aspects & requested_aspects
aspect_ids = allowed_aspects.map(&:id)
def people_in_aspects(requested_aspects, opts={})
allowed_aspects = aspects & requested_aspects
aspect_ids = allowed_aspects.map(&:id)

people = Person.in_aspects(aspect_ids)
people = Person.in_aspects(aspect_ids)

if opts[:type] == 'remote'
people = people.where(:owner_id => nil)
elsif opts[:type] == 'local'
people = people.where('people.owner_id IS NOT NULL')
if opts[:type] == "remote"
people = people.where(owner_id: nil)
elsif opts[:type] == "local"
people = people.where.not(people: {owner_id: nil})
end
people
end
people
end

def aspects_with_person(person)
contact_for(person).aspects
end
def aspects_with_person(person)
contact_for(person).aspects
end

def posts_from(person, with_order=true)
base_query = Post.from_person_visible_by_user(self, person)
return base_query.order("posts.created_at desc") if with_order
def posts_from(person, with_order=true)
base_query = Post.from_person_visible_by_user(self, person)
return base_query.order("posts.created_at desc") if with_order

base_query
end
base_query
end

def photos_from(person, opts={})
opts = prep_opts(Photo, opts)
Photo.from_person_visible_by_user(self, person)
.by_max_time(opts[:max_time])
.limit(opts[:limit])
end
def photos_from(person, opts={})
opts = prep_opts(Photo, opts)
Photo.from_person_visible_by_user(self, person)
.by_max_time(opts[:max_time])
.limit(opts[:limit])
end

protected

# @return [Hash]
def prep_opts(klass, opts)
defaults = {
order: "created_at DESC",
limit: 15,
hidden: false
}
defaults[:type] = Stream::Base::TYPES_OF_POST_IN_STREAM if klass == Post
opts = defaults.merge(opts)
opts.delete(:limit) if opts[:limit] == :all

opts[:order_field] = opts[:order].split.first.to_sym
opts[:order_with_table] = "#{klass.table_name}.#{opts[:order]}"

opts[:max_time] = Time.zone.at(opts[:max_time]) if opts[:max_time].is_a?(Integer)
opts[:max_time] ||= Time.zone.now + 1
opts
protected

# @return [Hash]
def prep_opts(klass, opts)
defaults = {
order: "created_at DESC",
limit: 15,
hidden: false
}
defaults[:type] = Stream::Base::TYPES_OF_POST_IN_STREAM if klass == Post
opts = defaults.merge(opts)
opts.delete(:limit) if opts[:limit] == :all

opts[:order_field] = opts[:order].split.first.to_sym
opts[:order_with_table] = "#{klass.table_name}.#{opts[:order]}"

opts[:max_time] = Time.zone.at(opts[:max_time]) if opts[:max_time].is_a?(Integer)
opts[:max_time] ||= Time.zone.now + 1
opts
end
end
end
Loading