Skip to content

Commit

Permalink
Add factory bot and tests for complete profile endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
renatolond committed Oct 30, 2024
1 parent b52363e commit 7ca939a
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require:
- rubocop-factory_bot
- rubocop-minitest
- rubocop-performance
- rubocop-rake
- rubocop-sequel
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ group :development, :test do
gem "pronto-rubocop", require: false # pronto-rubocop extends pronto for rubocop

gem "rubocop", require: false # A static code analyzer and formatter
gem "rubocop-factory_bot", require: false # A rubocop extension for factory bot
gem "rubocop-minitest", require: false # A rubocop extension for minitest
gem "rubocop-performance", require: false # A rubocop extension with performance suggestions
gem "rubocop-rake", require: false # A rubocop extension for Rakefiles
gem "rubocop-sequel", require: false # A rubocop extension for Sequel
Expand All @@ -54,6 +56,7 @@ group :test do

gem "database_cleaner-sequel" # Cleans the database between tests

gem "factory_bot" # makes it easier to create objects for tests
gem "faker" # provides fake data for tests
gem "mocha" # adds mocking capabilities
gem "webmock" # Used for avoiding real requests in the test enviroment
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ GEM
dry-inflector (~> 1.0)
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
factory_bot (6.5.0)
activesupport (>= 5.0.0)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
falcon (0.48.3)
Expand Down Expand Up @@ -232,6 +234,11 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
rubocop-factory_bot (2.23.1)
rubocop (~> 1.33)
rubocop-minitest (0.36.0)
rubocop (>= 1.61, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
Expand Down Expand Up @@ -280,6 +287,7 @@ DEPENDENCIES
database_cleaner-sequel
debug
dotenv
factory_bot
faker
falcon
grape (~> 2.2)
Expand All @@ -300,6 +308,8 @@ DEPENDENCIES
roda
rodauth
rubocop
rubocop-factory_bot
rubocop-minitest
rubocop-performance
rubocop-rake
rubocop-sequel
Expand Down
3 changes: 2 additions & 1 deletion config/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Database
class << self
# We use Falcon and need fiber concurrency for Sequel to behave well
Sequel.extension :fiber_concurrency
Sequel.extension :fiber_concurrency unless Environment.test?

# Connects to the database if no connection exists
#
Expand Down Expand Up @@ -57,6 +57,7 @@ def connection_options
connection_options = { extensions: %i[pg_array pg_enum] }
connection_options[:user] = ENV.fetch("PGSQL_USERNAME")
connection_options[:password] = ENV["PGSQL_PASSWORD"] if ENV["PGSQL_PASSWORD"]
connection_options[:max_connections] = 1 if Environment.test?
connection_options
end

Expand Down
51 changes: 45 additions & 6 deletions test/app/api/authenticated/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,58 @@

describe "get /profile/info" do
before do
password = "bogus123"
hash = BCrypt::Password.create(password, cost: BCrypt::Engine::MIN_COST)
login = "foo@retromeet.social"
account_id = Database.connection[:accounts].insert(email: login, status_id: 2)
Database.connection[:account_password_hashes].insert(id: account_id, password_hash: hash)
Database.connection[:account_informations].insert(account_id:, display_name: "Foo", created_at: Time.new(2024, 9, 20, 16, 50, 0))
password = "bogus123"
create(:account, email: login, password:, account_information: { display_name: "Foo", created_at: Time.new(2024, 9, 20, 16, 50, 0) })
@auth = login(login:, password:)
end

it "has the information expected" do
expected_response = { display_name: "Foo", created_at: Time.new(2024, 9, 20, 16, 50, 0) }
authorized_get @auth, "/api/profile/info"
assert last_response.ok?

assert_predicate last_response, :ok?
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
end

describe "get /profile/complete" do
before do
@endpoint = "/api/profile/complete"

login = "foo@retromeet.social"
password = "bogus123"
@account = create(:account, email: login, password:, account_information: { display_name: "Foo", created_at: Time.new(2024, 9, 20, 16, 50, 0) })
@auth = login(login:, password:)
end

require "date"
it "gets the user information" do
account_information = @account.account_information
expected_response = {
about_me: account_information.about_me,
created_at: account_information.created_at.iso8601,
birth_date: account_information.birth_date.to_s,
genders: account_information.genders,
orientations: account_information.orientations,
languages: account_information.languages,
relationship_status: account_information.relationship_status,
relationship_type: account_information.relationship_type,
tobacco: account_information.tobacco,
marijuana: account_information.marijuana,
alcohol: account_information.alcohol,
other_recreational_drugs: account_information.other_recreational_drugs,
pets: account_information.pets,
wants_pets: account_information.wants_pets,
kids: account_information.kids,
wants_kids: account_information.wants_kids,
religion: account_information.religion,
religion_importance: account_information.religion_importance,
display_name: account_information.display_name
}
authorized_get @auth, @endpoint

assert_predicate last_response, :ok?
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
end
Expand Down
22 changes: 22 additions & 0 deletions test/factories/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class Account < Sequel::Model
many_to_one :account_status, key: :status_id
one_to_one :account_password_hash
one_to_one :account_information
end

FactoryBot.define do
factory :account do
transient do
password { "password" }
account_information { {} }
end
email { Faker::Internet.email }
account_status { AccountStatus.verified }
after(:create) do |account, evaluator|
create(:account_password_hash, account:, password: evaluator.password)
create(:account_information, account:, **evaluator.account_information)
end
end
end
29 changes: 29 additions & 0 deletions test/factories/account_information.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class AccountInformation < Sequel::Model
many_to_one :account
end
FactoryBot.define do
factory :account_information do
account
about_me { Faker::Lorem.paragraph }
birth_date { Date.new(1985, 8, 5) }
genders { %w[questioning man] }
orientations { %w[bisexual fluid] }
languages { %w[eng por spa] }
relationship_status { "partnered" }
relationship_type { "non_monogamous" }
tobacco { "never" }
marijuana { "sometimes" }
alcohol { "sometimes" }
other_recreational_drugs { "sometimes" }
pets { "have" }
wants_pets { "maybe" }
kids { "have_not" }
wants_kids { "do_not_want_any" }
religion { "atheism" }
religion_importance { "not_important" }
display_name { Faker::Twitter.screen_name }
created_at { Time.now }
end
end
15 changes: 15 additions & 0 deletions test/factories/account_password_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class AccountPasswordHash < Sequel::Model
many_to_one :account, key: :id
end

FactoryBot.define do
factory :account_password_hash do
transient do
password { "password" }
end
account
password_hash { puts password; BCrypt::Password.create(password, cost: BCrypt::Engine::MIN_COST) }
end
end
15 changes: 15 additions & 0 deletions test/factories/account_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class AccountStatus < Sequel::Model
class << self
def verified
@verified ||= AccountStatus.where(name: "Verified").first
end
end
end

FactoryBot.define do
factory :account_status do
name { "status" }
end
end
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
require_relative "db_helper"
require_relative "rack_helper"

# Make factory_bot methods available in spec-style tests
module Minitest
class Spec
include FactoryBot::Syntax::Methods
end
end
FactoryBot.find_definitions
FactoryBot.define do
to_create(&:save_changes)
end

# Adapted from https://github.com/rails/rails/blob/97169912f197eee6e76fafb091113bddf624aa67/activesupport/lib/active_support/testing/assertions.rb#L101
# Test numeric difference between the return value of an expression as a
# result of what is evaluated in the yielded block.
Expand Down

0 comments on commit 7ca939a

Please sign in to comment.