added 260907 version of worker toolkit

This commit is contained in:
2026-09-08 20:30:51 -04:00
parent cbd3f0f8ca
commit 97aca37663
1283 changed files with 142951 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
require "rails_helper"
RSpec.describe Api::V1::CheckinsController do
let!(:user) { create(:user) }
let(:date) { "2016-01-06" }
before { sign_in user }
describe "index" do
context "when checkin exists for the requested date" do
before do
create(:checkin, date: Date.parse(date), user_id: user.id)
create(:checkin, date: Date.parse(date) + 1.hour, user_id: user.id)
create(:checkin, date: Date.parse(date) + 5.days, user_id: user.id)
create(:checkin, date: Date.parse(date) - 5.days, user_id: user.id)
end
it "returns correct checkin" do
get :index, params: {date: date}
expect(response_body[:checkins].count).to eq 2
expect(response_body[:checkins].pluck(:date).map(&:to_s).sort).to eq ["2016-01-06T00:00:00.000+00:00", "2016-01-06T01:00:00.000+00:00"]
returned_checkin = response_body[:checkins][0]
expect(Date.parse(returned_checkin[:date])).to eq Date.parse(date)
end
end
context "when checkin doesn't exist for the passed date" do
it "returns no results" do
get :index, params: {date: date}
expect(response_body[:checkins].count).to eq 0
end
end
end
describe "create" do
it "returns new checkin" do
travel_to(DateTime.new(2020, 1, 2, 3, 4, 5)) do
post :create, params: {checkin: {date: date}}
returned_checkin_1 = response_body[:checkin]
expect(DateTime.parse(returned_checkin_1[:date]).to_s).to eq "2016-01-06T03:04:05+00:00"
end
travel_to(DateTime.new(2020, 1, 2, 3, 4, 6)) do
post :create, params: {checkin: {date: date}}
returned_checkin_2 = response_body[:checkin]
expect(DateTime.parse(returned_checkin_2[:date]).to_s).to eq "2016-01-06T03:04:05+00:00"
end
travel_to(DateTime.new(2020, 1, 2, 3, 4, 7)) do
post :create, params: {checkin: {date: date}}
returned_checkin_3 = response_body[:checkin]
expect(DateTime.parse(returned_checkin_3[:date]).to_s).to eq "2016-01-06T03:04:05+00:00"
end
expect(user.last_checkin.date).to eq(DateTime.new(2016, 1, 6, 3, 4, 7))
end
end
describe "update" do
let(:checkin) { create(:checkin, user_id: user.id, date: Time.zone.today) }
let(:attributes) { {id: checkin.id, checkin: {note: "Blah"}} }
it "returns updated checkin" do
put :update, params: attributes
returned_checkin = response_body[:checkin]
expect(returned_checkin[:id]).to eq checkin.id.to_s
expect(returned_checkin[:note]).to eq attributes[:checkin][:note]
end
end
context "show" do
describe "for new user" do
let(:new_user) { create(:user) }
let(:checkin) { create(:checkin, user_id: new_user.id, date: Time.zone.today) }
let(:attributes) { {id: checkin.id} }
it "promotion visiability" do
get :show, params: attributes
returned_checkin = response_body[:checkin]
expect(returned_checkin[:available_for_promotion]).to be false
expect(returned_checkin[:promotion_skipped_at].blank?).to be true
expect(returned_checkin[:promotion_rate_id].blank?).to be true
end
end
describe "for user created_at 1 week ago" do
let(:old_user) { create(:user, created_at: 1.week.ago) }
context "Promotion rate" do
describe "start visiability" do
let(:checkin) { create(:checkin, user_id: old_user.id, date: Time.zone.today) }
let(:attributes) { {id: checkin.id} }
it "should be available" do
get :show, params: attributes
returned_checkin = response_body[:checkin]
expect(returned_checkin[:available_for_promotion]).to be true
expect(returned_checkin[:promotion_skipped_at].blank?).to be true
expect(returned_checkin[:promotion_rate_id].blank?).to be true
end
end
describe "skipped promotion rate" do
let(:checkin) { create(:checkin, user_id: old_user.id, promotion_skipped_at: Time.now.utc) }
let(:attributes) { {id: checkin.id} }
it "with skipped promotion rate" do
get :show, params: attributes
returned_checkin = response_body[:checkin]
expect(returned_checkin[:available_for_promotion]).to be false
expect(returned_checkin[:promotion_rate_id].blank?).to be true
end
end
describe "skipped promotion becomes available" do
let(:checkin) do
create(:checkin,
user_id: old_user.id,
date: Time.zone.today,
promotion_skipped_at: (Time.zone.today - 1.week))
end
let(:attributes) { {id: checkin.id} }
it "returns false after skipped promotion rate for 1 week" do
get :show, params: attributes
returned_checkin = response_body[:checkin]
expect(returned_checkin[:available_for_promotion]).to be true
end
end
describe "rated promotion becomes unavailable" do
let(:old_checkin) { create(:checkin, user_id: old_user.id, date: (Time.zone.today - 1.day)) }
let!(:promotion_rate) { create(:promotion_rate, checkin_id: old_checkin.id) }
let(:checkin) { create(:checkin, user_id: old_user.id, date: Time.zone.today) }
let(:attributes) { {id: checkin.id} }
it "returns false if user has rated" do
get :show, params: attributes
returned_checkin = response_body[:checkin]
expect(returned_checkin[:available_for_promotion]).to be false
end
end
end
end
end
end

View File

@@ -0,0 +1,47 @@
require "rails_helper"
RSpec.describe Api::V1::ConditionsController do
let!(:user) { create(:user) }
let!(:global_condition) { create(:condition) }
let!(:personal_condition) { create(:user_condition, user: user).condition }
let!(:another_user_condition) { create(:user_condition).condition }
let(:user_abilities) { Ability.new(user) }
let(:accessible_conditions) { Condition.accessible_by(user_abilities) }
before { sign_in user }
describe "index" do
it "returns all accessible conditions" do
get :index
returned_condition_ids = response_body[:conditions].map { |c| c[:id] }
accessible_condition_ids = accessible_conditions.map(&:id)
expect(returned_condition_ids.to_set).to eq accessible_condition_ids.to_set
end
end
describe "show" do
context "when condition is accessible" do
it "return the requested condition" do
get :show, params: {id: global_condition.id}
expect(response_body[:condition][:id]).to eq global_condition.id
end
end
context "when local condition is accessible" do
it "returns 200 (Authorized)" do
get :show, params: {id: another_user_condition.id}
expect(response.status).to eq 200
end
end
end
describe "create" do
let(:condition_attributes) { {name: "Headache"} }
it "creates the condition as personal for the current user" do
post :create, params: {condition: condition_attributes}
created_condition = response_body[:condition]
expect(created_condition[:name]).to eq condition_attributes[:name]
expect(Condition.find(created_condition[:id]).global).to be false
end
end
end

View File

@@ -0,0 +1,72 @@
require "rails_helper"
RSpec.describe Api::V1::CountriesController do
let(:test_country_id) { "IT" }
let(:test_country_name_en) { "Italy" }
let(:test_country_name_it) { "Italia" }
describe "index" do
let(:all_countries_count) { Country.all.count }
it "returns all countries" do
get :index
expect(response_body[:countries].size).to eq all_countries_count
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translations" do
get :index
test_country = response_body[:countries].find { |c| c[:id].eql?(test_country_id) }
expect(test_country[:name]).to eq test_country_name_en
end
end
context "with 'it' locale" do
before { I18n.default_locale = "it" }
it "returns 'it' translations" do
get :index
test_country = response_body[:countries].find { |c| c[:id].eql?(test_country_id) }
expect(test_country[:name]).to eq test_country_name_it
end
after { I18n.default_locale = "en" }
end
end
describe "show" do
it "returns test country" do
get :show, params: {id: test_country_id}
expect(response_body[:country][:id]).to eq test_country_id
end
it "returns test country case-insensitive" do
get :show, params: {id: test_country_id.downcase}
expect(response_body[:country][:id]).to eq test_country_id
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translation" do
get :show, params: {id: test_country_id}
expect(response_body[:country][:name]).to eq test_country_name_en
end
end
context "with 'it' locale" do
before { I18n.default_locale = "it" }
it "returns 'it' translation" do
get :show, params: {id: test_country_id}
expect(response_body[:country][:name]).to eq test_country_name_it
end
after { I18n.default_locale = "en" }
end
context "with unexisting country id" do
let(:unexisting_country_id) { "XY" }
it "returns 404 (Not Found)" do
get :show, params: {id: unexisting_country_id}
expect(response.status).to eq 404
end
end
context "with invalid country id" do
let(:unexisting_country_id) { "X1" }
it "returns 400 (Bad Request)" do
get :show, params: {id: unexisting_country_id}
expect(response.status).to eq 400
end
end
end
end

View File

@@ -0,0 +1,46 @@
require "rails_helper"
RSpec.describe Api::V1::DayHabitsController do
let(:test_day_habit_id) { "lying_down" }
let(:test_day_habit_name_en) { "Lying down" }
let(:test_day_habit_descr_en) { "bedridden" }
describe "index" do
let(:all_day_habits_count) { DayHabit.all.count }
it "returns all day_habits" do
get :index
expect(response_body[:day_habits].size).to eq all_day_habits_count
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translations" do
get :index
test_day_habit = response_body[:day_habits].find { |c| c[:id].eql?(test_day_habit_id) }
expect(test_day_habit[:name]).to eq test_day_habit_name_en
expect(test_day_habit[:description]).to eq test_day_habit_descr_en
end
end
end
describe "show" do
it "returns test day_habit" do
get :show, params: {id: test_day_habit_id}
expect(response_body[:day_habit][:id]).to eq test_day_habit_id
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translation" do
get :show, params: {id: test_day_habit_id}
expect(response_body[:day_habit][:name]).to eq test_day_habit_name_en
expect(response_body[:day_habit][:description]).to eq test_day_habit_descr_en
end
end
context "with invalid day_habit id" do
let(:invalid_day_habit_id) { "blah" }
it "returns 400 (Bad Request)" do
get :show, params: {id: invalid_day_habit_id}
expect(response.status).to eq 400
end
end
end
end

View File

@@ -0,0 +1,43 @@
require "rails_helper"
RSpec.describe Api::V1::EducationLevelsController do
let(:test_education_level_id) { "bachelors_degree" }
let(:test_education_level_name_en) { "Bachelor's degree" }
describe "index" do
let(:all_education_levels_count) { EducationLevel.all.count }
it "returns all education_levels" do
get :index
expect(response_body[:education_levels].size).to eq all_education_levels_count
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translations" do
get :index
test_education_level = response_body[:education_levels].find { |c| c[:id].eql?(test_education_level_id) }
expect(test_education_level[:name]).to eq test_education_level_name_en
end
end
end
describe "show" do
it "returns test education_level" do
get :show, params: {id: test_education_level_id}
expect(response_body[:education_level][:id]).to eq test_education_level_id
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translation" do
get :show, params: {id: test_education_level_id}
expect(response_body[:education_level][:name]).to eq test_education_level_name_en
end
end
context "with invalid education_level id" do
let(:invalid_education_level_id) { "blah" }
it "returns 400 (Bad Request)" do
get :show, params: {id: invalid_education_level_id}
expect(response.status).to eq 400
end
end
end
end

View File

@@ -0,0 +1,43 @@
require "rails_helper"
RSpec.describe Api::V1::EthnicitiesController do
let(:test_ethnicity_id) { "south_asian" }
let(:test_ethnicity_name_en) { "South Asian" }
describe "index" do
let(:all_ethnicities_count) { Ethnicity.all.count }
it "returns all ethnicities" do
get :index
expect(response_body[:ethnicities].size).to eq all_ethnicities_count
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translations" do
get :index
test_ethnicity = response_body[:ethnicities].find { |c| c[:id].eql?(test_ethnicity_id) }
expect(test_ethnicity[:name]).to eq test_ethnicity_name_en
end
end
end
describe "show" do
it "returns test ethnicity" do
get :show, params: {id: test_ethnicity_id}
expect(response_body[:ethnicity][:id]).to eq test_ethnicity_id
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translation" do
get :show, params: {id: test_ethnicity_id}
expect(response_body[:ethnicity][:name]).to eq test_ethnicity_name_en
end
end
context "with invalid ethnicity id" do
let(:invalid_ethnicity_id) { "blah" }
it "returns 400 (Bad Request)" do
get :show, params: {id: invalid_ethnicity_id}
expect(response.status).to eq 400
end
end
end
end

View File

@@ -0,0 +1,96 @@
require "rails_helper"
RSpec.describe Api::V1::PasswordsController do
let(:user) { create(:user, password: "ValidPassword123", password_confirmation: "ValidPassword123") }
describe "create" do
context "when attempting to reset a password" do
let(:password_params) do
{
# Match the form posted by the front end
current_password: nil,
email: nil,
password: nil,
password_confirmation: nil,
reset_password_token: nil
}
end
context "for an email that exists in the database" do
it "returns a password reset object" do
expect_any_instance_of(User).to receive(:send_reset_password_instructions).and_return true
post :create, params: {password: password_params.merge(email: user.email)}
expect(response.status).to eq 200
expect(response_body[:password]).to include({"email" => user.email})
end
it "finds a user by case-insensitive email address" do
other_user = create :user, email: "arEALLycAMELcaSeemAIl@gmail.com"
expect_any_instance_of(User).to receive(:send_reset_password_instructions).and_return true
post :create, params: {password: password_params.merge(email: "AReallyCamelCaseEmail@GmAiL.CoM")}
expect(response.status).to eq 200
expect(response_body[:password]).to include({"email" => other_user.email})
end
end
context "for an email that does not exist in the database" do
it "returns a 404" do
expect_any_instance_of(User).to_not receive(:send_reset_password_instructions)
post :create, params: {password: password_params.merge(email: "Idonotexist@example.com")}
expect(response.status).to eq 404
end
end
end
end
describe "update" do
context "when no user logged-in" do
context "when missing token" do
it "returns 422 (Unprocessable Entity)" do
put :update, params: {id: 1}
expect(response.status).to eq 422
end
end
context "when provide token" do
it "returns password " do
put :update, params: {id: 1, token: "123"}
expect(response.status).to eq 422
end
end
end
context "when user logged-in" do
before { sign_in user }
context "when provide right params" do
it "update password" do
put(:update, params: {id: user.id, password: {
current_password: "ValidPassword123",
password: "password123",
password_confirmation: "password123"
}})
expect(response_body[:password][:email]).to eq user.email
end
end
context "when password confirmation is wrong" do
it "returns 422 (Unprocessable Entity)" do
put :update, params: {id: user.id, password: {password: "password123", password_confirmation: "password"}}
expect(response.status).to eq 422
end
end
context "when current password is wrong" do
it "returns 422 (Unprocessable Entity)" do
put(:update, params: {id: user.id, password: {
current_password: "InvalidPassword123",
password: "password123",
password_confirmation: "password123"
}})
expect(response.status).to eq 422
end
end
end
end
end

View File

@@ -0,0 +1,40 @@
require "rails_helper"
RSpec.describe Api::V1::PostsController do
let(:user) { create(:user) }
let(:other_user) { create(:user) }
before { sign_in user }
describe "index" do
it "returns a list of discussion posts" do
create(:post, user_id: user.id)
create(:post, user_id: other_user.id)
get :index
expect(response_body[:posts].size).to eq 2
end
it "returns a list of discussion posts filtered by post type" do
post = create(:post, user_id: user.id)
create(:post, user_id: other_user.id)
symptom = post.symptom_ids
get :index, params: {id: symptom[0], type: "symptom"}
expect(response_body[:posts].size).to eq 1
end
it "returns a list of discussion posts filtered by topic followings" do
post = create(:post, user_id: user.id)
create(:post, user_id: user.id)
create(:post, user_id: user.id)
topic_user_is_following = user.topic_following
topic_user_is_following.symptom_ids = post.symptom_ids
topic_user_is_following.save
get :index, params: {following: true}
expect(response_body[:posts].size).to eq 1
end
end
end

View File

@@ -0,0 +1,85 @@
require "rails_helper"
RSpec.describe Api::V1::ProfilesController do
let(:user) { create(:user) }
let(:profile) { user.profile }
let(:another_user) { create(:user) }
let(:another_profile) { another_user.profile }
describe "show" do
context "when no user logged-in" do
it "returns 302 (Permanent Redirect)" do
get :show, params: {id: profile.id}
expect(response.status).to eq 302
end
end
context "when different user logged-in" do
before { sign_in another_user }
it "returns 401 (unauthorized)" do
get :show, params: {id: profile.id}
expect(response.status).to eq 401
end
end
context "when user logged-in" do
before { sign_in user }
it "returns profile" do
get :show, params: {id: profile.id}
expect(response_body[:profile][:id]).to eq profile.id
end
context "with invalid profile id" do
it "returns 404 (RecordNotFound)" do
get :show, params: {id: "a123"}
expect(response.status).to eq 404
end
end
context "when different profile id is requested" do
it "returns 401 (unauthorized)" do
get :show, params: {id: another_profile.id}
expect(response.status).to eq 401
end
end
end
end
describe "update" do
context "when user logged-in" do
before { sign_in user }
let(:profile_attributes) { attributes_for(:profile) }
it "updates profile attributes" do
put :update, params: {id: profile.id, profile: profile_attributes}
updated_profile = response_body[:profile]
expect(updated_profile[:id]).to eq profile.id
expect(updated_profile[:country_id]).to eq profile_attributes[:country_id]
expect(updated_profile[:sex_id]).to eq profile_attributes[:sex_id]
expect(updated_profile[:birth_date]).to eq profile_attributes[:birth_date].to_date.to_s
end
context "with available locale for country" do
before { profile_attributes[:country_id] = "IT" }
it "sets the first country's language as locale" do
put :update, params: {id: profile.id, profile: profile_attributes}
expect(I18n.locale).to eq :it
end
after { I18n.locale = :en }
end
context "with unavailable locale for country" do
before { profile_attributes[:country_id] = "IN" }
it "sets locale to default" do
put :update, params: {id: profile.id, profile: profile_attributes}
expect(I18n.locale).to eq I18n.default_locale
end
end
context "when some attribute is invalid" do
before { profile_attributes[:sex_id] = "blah" }
it "returns 422 (unprocessable_entity)" do
put :update, params: {id: profile.id, profile: profile_attributes}
expect(response.status).to eq 422
end
end
end
end
end

View File

@@ -0,0 +1,56 @@
require "rails_helper"
RSpec.describe Api::V1::RegistrationsController do
let(:attributes) do
{
email: "user@example.com",
password: "secret123",
password_confirmation: "secret123",
screen_name: "user",
captcha_response: "dummy-captcha-response"
}
end
let(:user) { create(:user, attributes.slice(:email, :password, :password_confirmation)) }
describe "create" do
let(:registration) do
new_registration = Registration.new(
ActionController::Parameters.new(registration: attributes)
)
new_registration.user = user
new_registration
end
before { allow(Registration).to receive(:create!).and_return(registration) }
it "registers a new user" do
post :create, params: {registration: attributes}
expect(response.status).to eq 200
user = response_body[:users][0]
expect(user[:email]).to eq attributes[:email]
end
end
let(:attributes_for_destroy) do
{
delete_reason: "Reason",
email: user.email
}
end
describe "destroy" do
let!(:checkin_with_ref) do
create(:checkin, date: Date.parse("2016-01-06"), user_id: user.id, position_id: 1, postal_code: "123456789")
end
it "removes user and it's profile" do
delete :destroy, params: attributes_for_destroy
expect(response.status).to eq 200
expect(Feedback.last.delete_reason).to eq attributes_for_destroy[:delete_reason]
expect(checkin_with_ref.reload.postal_code).to be_nil
expect(checkin_with_ref.reload.position).to be_nil
expect(User.find_by(id: user.id)).to be_nil
expect(Profile.find_by(user_id: user.id)).to be_nil
end
end
end

View File

@@ -0,0 +1,40 @@
require "rails_helper"
RSpec.describe Api::V1::SearchesController do
before { I18n.default_locale = :en }
let!(:user) { create(:user) }
let!(:foods) { create_list(:food, 2) }
let!(:conditions) { create_list(:condition, 2) }
before { sign_in user }
describe "show" do
context "when searchable exists for the requested date" do
let!(:condition_to_find) { create(:condition, name: "ACL injury") }
it "returns correct searchables with first three letters" do
get :show, params: {resource: "condition", query: {name: "ACL"}}
expect_valid_responses(response_body)
end
def expect_valid_responses(response)
expect(response[:search][:searchables].count).to eq 1
expect(response[:search][:searchables][0][:type]).to eq "condition"
expect(response[:search][:searchables][0][:name]).to eq "ACL injury"
end
end
context "when food exists" do
let!(:food_to_find) { create(:food, long_desc: "Banana") }
before { get :show, params: {resource: "food", query: {name: "bana"}} }
it "returns correct searchable food" do
expect(response_body[:search][:searchables].count).to eq 1
expect(response_body[:search][:searchables][0][:type]).to eq "food"
expect(response_body[:search][:searchables][0][:name]).to eq "Banana"
end
end
end
end

View File

@@ -0,0 +1,61 @@
require "rails_helper"
RSpec.describe Api::V1::SexesController do
let(:test_sex_id) { "male" }
let(:test_sex_name_en) { "Male" }
let(:test_sex_name_it) { "Maschio" }
describe "index" do
let(:all_sexes_count) { Sex.all.count }
it "returns all sexes" do
get :index
expect(response_body[:sexes].size).to eq all_sexes_count
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translations" do
get :index
test_sex = response_body[:sexes].find { |c| c[:id].eql?(test_sex_id) }
expect(test_sex[:name]).to eq test_sex_name_en
end
end
context "with 'it' locale" do
before { I18n.default_locale = "it" }
it "returns 'it' translations" do
get :index
test_sex = response_body[:sexes].find { |c| c[:id].eql?(test_sex_id) }
expect(test_sex[:name]).to eq test_sex_name_it
end
after { I18n.default_locale = "en" }
end
end
describe "show" do
it "returns test sex" do
get :show, params: {id: test_sex_id}
expect(response_body[:sex][:id]).to eq test_sex_id
end
context "with 'en' locale" do
before { I18n.default_locale = "en" }
it "returns 'en' translation" do
get :show, params: {id: test_sex_id}
expect(response_body[:sex][:name]).to eq test_sex_name_en
end
end
context "with 'it' locale" do
before { I18n.default_locale = "it" }
it "returns 'it' translation" do
get :show, params: {id: test_sex_id}
expect(response_body[:sex][:name]).to eq test_sex_name_it
end
after { I18n.default_locale = "en" }
end
context "with invalid sex id" do
let(:invalid_sex_id) { "blah" }
it "returns 400 (Bad Request)" do
get :show, params: {id: invalid_sex_id}
expect(response.status).to eq 400
end
end
end
end

View File

@@ -0,0 +1,47 @@
require "rails_helper"
RSpec.describe Api::V1::SymptomsController do
let!(:user) { create(:user) }
let!(:global_symptom) { create(:symptom) }
let!(:personal_symptom) { create(:user_symptom, user: user).symptom }
let!(:another_user_symptom) { create(:user_symptom).symptom }
let(:user_abilities) { Ability.new(user) }
let(:accessible_symptoms) { Symptom.accessible_by(user_abilities) }
before { sign_in user }
describe "index" do
it "returns all accessible symptoms" do
get :index
returned_symptom_ids = response_body[:symptoms].map { |c| c[:id] }
accessible_symptom_ids = accessible_symptoms.map(&:id)
expect(returned_symptom_ids.to_set).to eq accessible_symptom_ids.to_set
end
end
describe "show" do
context "when symptom is accessible" do
it "return the requested symptom" do
get :show, params: {id: global_symptom.id}
expect(response_body[:symptom][:id]).to eq global_symptom.id
end
end
context "when local symptom is accessible" do
it "returns 200 (Authorized)" do
get :show, params: {id: another_user_symptom.id}
expect(response.status).to eq 200
end
end
end
describe "create" do
let(:symptom_attributes) { {name: "Headache"} }
it "creates the symptom as personal for the current user" do
post :create, params: {symptom: symptom_attributes}
created_symptom = response_body[:symptom]
expect(created_symptom[:name]).to eq symptom_attributes[:name]
expect(Symptom.find(created_symptom[:id]).global).to be false
end
end
end

View File

@@ -0,0 +1,45 @@
require "rails_helper"
RSpec.describe Api::V1::TagsController do
let!(:user) { create(:user) }
let!(:tag) { create(:tag) }
let(:user_abilities) { Ability.new(user) }
let(:accessible_tags) { Tag.accessible_by(user_abilities) }
before { sign_in user }
describe "index" do
it "returns all accessible tags" do
get :index
returned_tag_ids = response_body[:tags].map { |c| c[:id] }
accessible_tag_ids = accessible_tags.map(&:id)
expect(returned_tag_ids.to_set).to eq accessible_tag_ids.to_set
end
context "with ids param" do
let(:tag_ids) { create_list(:tag, 2).map(&:id) }
it "returns requested tags only" do
get :index, params: {ids: tag_ids}
returned_tag_ids = response_body[:tags].map { |c| c[:id] }
expect(returned_tag_ids.to_set).to eq tag_ids.to_set
end
end
end
describe "show" do
it "returns the requested tag" do
get :show, params: {id: tag.id}
expect(response_body[:tag][:id]).to eq tag.id
end
end
describe "create" do
let(:tag_attributes) { {name: "had sex"} }
it "creates the tag" do
post :create, params: {tag: tag_attributes}
created_tag = response_body[:tag]
expect(created_tag[:name]).to eq tag_attributes[:name]
expect(Tag.find(created_tag[:id]).global).to be false
end
end
end

View File

@@ -0,0 +1,88 @@
require "rails_helper"
RSpec.describe Api::V1::TrackingsController do
let!(:user) { create(:user) }
let!(:current_tracking) { create(:tracking, :active, :for_condition, user: user) }
let!(:old_tracking) { create(:tracking, :inactive, :for_symptom, user: user) }
let(:another_user_tracking) { create(:tracking, :active, :for_condition) }
before { sign_in user }
describe "index" do
context "at current time" do
let(:trackable_type) { current_tracking.trackable_type }
let(:at) { Time.zone.now }
it "returns current trackings only" do
get :index, params: {at: at, trackable_type: trackable_type}
returned_ids = response_body[:trackings].map { |t| t[:id] }
expect(returned_ids).to include current_tracking.id
expect(returned_ids).not_to include old_tracking.id
end
end
context "at past time" do
let(:trackable_type) { old_tracking.trackable_type }
let(:at) { 5.days.ago }
it "returns old trackings only" do
get :index, params: {at: at, trackable_type: trackable_type}
returned_ids = response_body[:trackings].map { |t| t[:id] }
expect(returned_ids).to include old_tracking.id
expect(returned_ids).not_to include current_tracking.id
end
end
end
describe "show" do
context "when a current user's tracking is requested" do
it "returns the requested tracking" do
get :show, params: {id: current_tracking.id}
expect(response_body[:tracking][:id]).to eq current_tracking.id
end
end
context "when another user's tracking is requested" do
it "returns 401 (Unauthorized)" do
get :show, params: {id: another_user_tracking.id}
expect(response.status).to eq 401
end
end
end
describe "create" do
let(:symptom) { create(:symptom) }
let(:tracking_attributes) { {trackable_id: symptom.id, trackable_type: symptom.class.name} }
it "saves a current tracking record" do
post :create, params: {tracking: tracking_attributes}
expect(response_body[:tracking][:id]).to be_present
expect(response_body[:tracking][:user_id]).to eq user.id
expect(response_body[:tracking][:trackable_id]).to eq symptom.id
expect(response_body[:tracking][:trackable_type]).to eq symptom.class.name
start_at = Date.parse(response_body[:tracking][:start_at])
expect(start_at.strftime("%F")).to eq Time.zone.today.strftime("%F")
end
end
describe "destroy" do
context "when destroying a current user's tracking" do
context "when tracking started in the past" do
before { current_tracking.update!(start_at: Time.zone.today - 2.days) }
it "sets end_date on that tracking" do
delete :destroy, params: {id: current_tracking.id}
end_at = current_tracking.reload.end_at
expect(end_at).to be_present
expect(end_at.strftime("%F")).to eq Time.zone.now.strftime("%F")
end
end
context "when tracking started today" do
it "destroys that tracking" do
delete :destroy, params: {id: current_tracking.id}
expect(Tracking.find_by(id: current_tracking.id)).to be_nil
end
end
end
context "when destroying another user's tracking" do
it "returns 401 (Unauthorized)" do
delete :destroy, params: {id: another_user_tracking.id}
expect(response.status).to eq 401
end
end
end
end

View File

@@ -0,0 +1,47 @@
require "rails_helper"
RSpec.describe Api::V1::TreatmentsController do
let!(:user) { create(:user) }
let!(:global_treatment) { create(:treatment) }
let!(:personal_treatment) { create(:user_treatment, user: user).treatment }
let!(:another_user_treatment) { create(:user_treatment).treatment }
let(:user_abilities) { Ability.new(user) }
let(:accessible_treatments) { Treatment.accessible_by(user_abilities) }
before { sign_in user }
describe "index" do
it "returns all accessible treatments" do
get :index
returned_treatment_ids = response_body[:treatments].map { |c| c[:id] }
accessible_treatment_ids = accessible_treatments.map(&:id)
expect(returned_treatment_ids.to_set).to eq accessible_treatment_ids.to_set
end
end
describe "show" do
context "when treatment is accessible" do
it "return the requested treatment" do
get :show, params: {id: global_treatment.id}
expect(response_body[:treatment][:id]).to eq global_treatment.id
end
end
context "when local treatment is accessible" do
it "returns 200 (Authorized)" do
get :show, params: {id: another_user_treatment.id}
expect(response.status).to eq 200
end
end
end
describe "create" do
let(:treatment_attributes) { {name: "Aspirin"} }
it "creates the treatment as personal for the current user" do
post :create, params: {treatment: treatment_attributes}
created_treatment = response_body[:treatment]
expect(created_treatment[:name]).to eq treatment_attributes[:name]
expect(Treatment.find(created_treatment[:id]).global).to be false
end
end
end

View File

@@ -0,0 +1,66 @@
require "rails_helper"
RSpec.describe Api::V1::UsersController do
let(:user) { create(:user) }
let(:another_user) { create(:user) }
describe "show" do
context "when no user logged-in" do
it "returns 302 (Permanent Redirect)" do
get :show, params: {id: user.id}
expect(response.status).to eq 302
end
end
context "when different user logged-in" do
before { sign_in another_user }
it "returns 401 (unauthorized)" do
get :show, params: {id: user.id}
expect(response.status).to eq 401
end
end
context "when user logged-in" do
before { sign_in user }
it "returns user" do
get :show, params: {id: user.id}
expect(response_body[:user][:id]).to eq user.id
end
context "with invalid user id" do
it "returns 404 (RecordNotFound)" do
get :show, params: {id: "a123"}
expect(response.status).to eq 404
end
end
context "when different user id is requested" do
it "returns 401 (unauthorized)" do
get :show, params: {id: another_user.id}
expect(response.status).to eq 401
end
end
end
end
describe "update" do
context "with valid email" do
before { sign_in user }
it "allow user ti change email" do
put :update, params: {id: user.id, user: {email: "some_email@ex.com"}}
expect(response.status).to eq 200
expect(user.reload.email).to eq "some_email@ex.com"
end
end
context "with invalid email" do
before { sign_in user }
it "returns an error" do
put :update, params: {id: user.id, user: {email: another_user.email}}
expect(response.status).to eq 422
end
end
end
end

View File

@@ -0,0 +1,44 @@
require "rails_helper"
describe Api::V1::WeathersController do
let(:user) { create(:user) }
let(:date) { "2016-01-06" }
let(:postal_code) { "55403" }
let(:index_action) { get :index, params: {date: date, postal_code: postal_code, format: :js} }
context "anonymous user" do
describe "index" do
it "should not hit weather API" do
expect(WeatherRetriever).not_to receive(:get)
index_action
end
it "should not authorize" do
index_action
expect(response).to have_http_status(:unauthorized)
end
end
end
context "user is present" do
before { sign_in user }
describe "index" do
let(:weather) { create :weather }
let(:json_response) { JSON.parse(response.body, symbolize_names: true) }
let(:expected_keys) { %i[id humidity icon precip_intensity pressure temperature_max temperature_min] }
let(:not_expected_keys) { %i[date postal_code] }
before { expect(WeatherRetriever).to receive(:get).with(Date.parse(date), postal_code).and_return(weather) }
before { index_action }
subject { json_response[:weather].keys }
it { is_expected.to include(*expected_keys) }
it { is_expected.not_to include(*not_expected_keys) }
it { expect(response).to have_http_status(:ok) }
end
end
end