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

Validate user is mentee for wish v2 #216

Merged
merged 7 commits into from
Jan 12, 2020
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
2 changes: 2 additions & 0 deletions server/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ group :development, :test do
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'capybara', '~> 3.30'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'selenium-webdriver'
gem 'rspec-rails', '~> 3.9'
end
Expand Down
2 changes: 1 addition & 1 deletion server/app/controllers/api/v1/api_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def destroy

def api_user_params

params.require(:api_user).permit(:first_name, :last_name, :city, :email, :password_digest, :username, :mentor, :mentee)
params.require(:api_user).permit(:first_name, :last_name, :city, :email, :password_digest, :mentor, :mentee)
end

def set_api_user
Expand Down
2 changes: 1 addition & 1 deletion server/app/controllers/api/v1/wishes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def destroy
private

def wish_params
params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id)
params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id, :mentee)
end

def set_api_user
Expand Down
15 changes: 13 additions & 2 deletions server/app/models/api_user.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
class ApiUser < ApplicationRecord
# encrypt password
#has_secure_password


# Can write model association here for example
has_many :wishes, dependent: :destroy
# validation
validates_presence_of :email, :password_digest, :username, :mentor, :mentee

validates_presence_of :email, :password_digest, :username
validates :email, uniqueness: true, on: [:create, :update]
validates :username, uniqueness: true, on: [:create, :update]
validate :mentor_or_mentee


def mentor_or_mentee
unless mentor && mentee
errors.add(:api_user, "must either want to be a mentor or mentee or both")
end
end

end


20 changes: 18 additions & 2 deletions server/app/models/wish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ class Wish < ApplicationRecord
delegate :language, :to => :programminglanguage
belongs_to :meetinginterval
delegate :interval, :to => :meetinginterval
# validation
validates_presence_of :goal
validates_presence_of :api_user_id
validate :validate_mentee
validate :online_or_offline

#This method does not need to be called and will show the program that
#tries to assign this value, this is the way figured out what was
#happening with validate mentee
# def api_user_id=(value)
# byebug
# super(value)
# end

def online_or_offline
if (available_offline == false) && (available_online == false)
if (available_offline == false) && (available_online == false)
errors.add(:api_user, "must be available either online or offline to complete wish")
end
end

def validate_mentee
if (self.api_user.nil? || self.api_user.mentee == false)
errors.add(:api_user, "if you are creating a wish you must be a mentee")
end
end
end


3 changes: 0 additions & 3 deletions server/app/views/api/v1/wishes/_wish.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@


json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval)


3 changes: 1 addition & 2 deletions server/app/views/api/v1/wishes/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


json.array! @wishes do |wish|
json.partial! 'wish', wish: wish
end

4 changes: 2 additions & 2 deletions server/db/migrate/20190623093635_create_api_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def change
t.string :email
t.string :password_digest

t.boolean :mentor
t.boolean :mentee
t.boolean :mentor, null: false
t.boolean :mentee, null: false
t.timestamps
end
end
Expand Down
6 changes: 6 additions & 0 deletions server/db/migrate/20191228155500_add_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIndex < ActiveRecord::Migration[6.0]
def change
add_index :api_users, [:email, :mentor, :mentee]

end
end
7 changes: 4 additions & 3 deletions server/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_10_20_165743) do
ActiveRecord::Schema.define(version: 2019_12_28_155500) do

create_table "api_users", force: :cascade do |t|
t.string "first_name"
Expand All @@ -19,10 +19,11 @@
t.string "city"
t.string "email"
t.string "password_digest"
t.boolean "mentor"
t.boolean "mentee"
t.boolean "mentor", null: false
t.boolean "mentee", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email", "mentor", "mentee"], name: "index_api_users_on_email_and_mentor_and_mentee"
end

create_table "meetingintervals", force: :cascade do |t|
Expand Down
13 changes: 8 additions & 5 deletions server/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
{first_name: 'Bob', last_name: 'Dylan', city: 'New York', mentor: true, mentee: true, email: '[email protected]', password_digest: 'password2', username: 'user2'},
{first_name: 'Jane', last_name: 'Smith', city: 'Edinburgh', mentor: false, mentee: true, email: '[email protected]', password_digest: 'password3', username: 'user3' }
])
Meetinginterval.create([{interval: 'weekly'}, {interval: 'daily'}])
Programminglanguage.create([{language: 'python'},{language: 'SCALA'}])
weekly_meet = Meetinginterval.create(interval: 'weekly')
daily_meet = Meetinginterval.create(interval: 'daily')
python_lang = Programminglanguage.create(language: 'python')
scala_lang = Programminglanguage.create(language: 'SCALA')

users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good', programminglanguage: python_lang, meetinginterval: weekly_meet}])
users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter', programminglanguage: python_lang, meetinginterval: daily_meet}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python', programminglanguage: scala_lang, meetinginterval: weekly_meet}])

users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good', programminglanguage_id: Programminglanguage.second.id, meetinginterval_id: Meetinginterval.first.id}])
users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.first.id}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.second.id}])

p "Created #{ApiUser.count} api_user entries"
p "Created #{Meetinginterval.count} meeting interval entries"
Expand Down
1 change: 1 addition & 0 deletions server/spec/factories/api_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
mentee {Faker::Boolean.boolean(true_ratio: 1)}
end
end

3 changes: 2 additions & 1 deletion server/spec/factories/wishes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do

factory :wish do
available_offline {Faker::Boolean.boolean(true_ratio: 0.5)}
available_offline {Faker::Boolean.boolean(true_ratio: 1)}
available_online {Faker::Boolean.boolean(true_ratio: 1)}
# it is invalid for available_offline and available_online to both be set to false.
# therefore when generating test data for spec one value is always set to true.
Expand Down
23 changes: 17 additions & 6 deletions server/spec/models/api_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
RSpec.describe ApiUser, type: :model do
# Association test
# ensure an item record has 1:m relationship with the *** model
it { should have_many(:wishes).dependent(:destroy) }

# Validation test
# ensure columns 'email' and 'password' are present before

it {should validate_presence_of(:email)}
it {should validate_presence_of(:password_digest)}
describe 'validations' do
subject {build(:api_user, mentor: false, mentee: false)}

# Validation test
it { should have_many(:wishes).dependent(:destroy) }
it {should validate_presence_of(:email)}
it {should validate_presence_of(:password_digest)}
it {should validate_presence_of(:username)}
it {should validate_uniqueness_of(:email).on(:create)}
it {should validate_uniqueness_of(:email).on(:update)}
it {should validate_uniqueness_of(:username).on(:create)}
it {should validate_uniqueness_of(:username).on(:update)}
it 'validations custom' do
api_user1 = ApiUser.new(mentor: false, mentee: false)
expect(api_user1).to be_invalid
expect(api_user1.errors.full_messages).to include('Api user must either want to be a mentor or mentee or both')
end
end
end
37 changes: 28 additions & 9 deletions server/spec/models/wish_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
require 'rails_helper'

RSpec.describe Wish, type: :model do
# Association test
# ensure an item record belongs to a single todo record
it { should belong_to(:api_user) }

# Validation test
it { should validate_presence_of(:goal) }
let!(:api_user){
#puts "hi"
create(:api_user, mentee: true)}
let!(:prog){create(:programminglanguage)}
let!(:meet){create(:meetinginterval)}

describe 'validations' do
subject {create(:wish, api_user_id: api_user.id, programminglanguage_id: prog.id, meetinginterval_id: meet.id)}
it {should validate_presence_of(:goal)}
it {should validate_presence_of(:api_user_id)}
it {should belong_to(:api_user)}
it {should belong_to(:programminglanguage)}
it {should belong_to(:meetinginterval)}
it {should delegate_method(:language).to(:programminglanguage)}
it {should delegate_method(:interval).to(:meetinginterval)}
it "validations custom 1" do
wish1 = Wish.new(api_user_id: api_user.id, programminglanguage_id: prog.id, meetinginterval_id: meet.id, available_offline: false, available_online: :false)
expect(wish1).to be_invalid
expect(wish1.errors.full_messages).to include('Api user must be available either online or offline to complete wish')
end
it "validations custom 2" do
wish2 = Wish.new(api_user_id: api_user.id, programminglanguage_id: prog.id, meetinginterval_id: meet.id, available_offline: false, available_online: :false)
wish2.api_user.mentee = false
expect(wish2).to be_invalid
expect(wish2.errors.full_messages).to include('Api user if you are creating a wish you must be a mentee')
end
end

it "both online and offline should not be false" do
example_a = build(:wish, available_offline: false, available_online: false)
expect(example_a).to_not be_valid
end
end


2 changes: 1 addition & 1 deletion server/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

config.before(:suite) do
DatabaseCleaner.clean_with(:truncation,
except: %w(ar_internal_metadata)
except: %w(ar_internal_metadata)
)
DatabaseCleaner.strategy = :transaction
end
Expand Down
2 changes: 1 addition & 1 deletion server/spec/requests/api/v1/wishes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:api_user_id){api_user.id}
let(:id){wishes.first.id}


# Test suite for GET /api/v1/api_users/api_user_id/wishes
describe 'GET /api/v1/api_users/api_users_id/wishes}' do
before {get "/api/v1/api_users/#{api_user_id}/wishes/"}
Expand Down