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

Api prog and meeting part 2 #194

Merged
merged 11 commits into from
Jan 12, 2020
3 changes: 3 additions & 0 deletions server/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver

gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'capybara', '~> 3.29'
gem 'selenium-webdriver'
gem 'rspec-rails', '~> 3.9'
Expand Down
16 changes: 16 additions & 0 deletions server/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
bcrypt (3.1.13)
bindex (0.8.1)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bootsnap (1.4.5)
msgpack (~> 1.0)
builder (3.2.3)
Expand All @@ -73,6 +75,7 @@ GEM
regexp_parser (~> 1.5)
xpath (~> 3.2)
childprocess (3.0.0)
coderay (1.1.2)
coffee-rails (5.0.0)
coffee-script (>= 2.2.0)
railties (>= 5.2.0)
Expand All @@ -83,6 +86,7 @@ GEM
concurrent-ruby (1.1.5)
crass (1.0.5)
database_cleaner (1.7.0)
debug_inspector (0.0.3)
diff-lcs (1.3)
erubi (1.9.0)
execjs (2.7.0)
Expand All @@ -98,6 +102,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
interception (0.5)
jbuilder (2.9.1)
activesupport (>= 4.2.0)
jwt (2.2.1)
Expand All @@ -121,6 +126,15 @@ GEM
nokogiri (1.10.4)
mini_portile2 (~> 2.4.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-rescue (1.5.0)
interception (>= 0.5)
pry (>= 0.12.0)
pry-stack_explorer (0.4.9.3)
binding_of_caller (>= 0.7)
pry (>= 0.9.11)
public_suffix (4.0.1)
puma (4.2.1)
nio4r (~> 2.0)
Expand Down Expand Up @@ -241,6 +255,8 @@ DEPENDENCIES
jwt
listen (>= 3.0.5, < 3.3)
pg
pry-rescue
pry-stack_explorer
puma (~> 4.2)
rails (~> 6.0)
rspec-rails (~> 3.9)
Expand Down
4 changes: 2 additions & 2 deletions server/app/models/api_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class ApiUser < ApplicationRecord
# Can write model association here for example
has_many :wishes, dependent: :destroy
# validation
validates_presence_of :email, :password_digest, :username
validates :email, uniqueness: true, on: :create and :update
validates_presence_of :email, :password_digest, :username, :mentor, :mentee
validates :email, uniqueness: true, on: [:create, :update]
validates :username, uniqueness: true, on: [:create, :update]
end

4 changes: 2 additions & 2 deletions server/spec/factories/api_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
last_name {Faker::Name.last_name}
username {Faker::Name.name}
city {Faker::Address.city}
mentor {Faker::Boolean.boolean(true_ratio: 0.5)}
mentee {Faker::Boolean.boolean(true_ratio: 0.5)}
mentor {Faker::Boolean.boolean(true_ratio: 1)}
mentee {Faker::Boolean.boolean(true_ratio: 1)}
end
end
205 changes: 90 additions & 115 deletions server/spec/requests/api/v1/api_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@


# initialize test data
let!(:api_users){create_list(:api_user, 10)}
let(:api_user_id) {api_users.first.id}
let!(:api_users){create_list(:api_user, 2)}
let!(:api_user_id) {api_users.first.id}


# Test suite for GET /api/v1/api_users
describe 'GET /api/v1/api_users' do
before {get "/api/v1/api_users"}

it 'returns api_users' do
expect(json).not_to be_empty
expect(json.size).to eq(10)
expect(json.size).to eq(2)
end

it 'returns status code 200' do
Expand Down Expand Up @@ -47,33 +48,28 @@
expect(json['message']).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/)
end
end

end

# Test suite POST /api/v1/api_user
describe 'POST /api/v1/api_users' do
let(:valid_attributes) do
# send json payload
{ 'email': '[email protected]', 'password_digest': 'password1', 'username': 'user11'}.to_json

context 'when request is valid' do
before { post '/api/v1/api_users', params: valid_attributes}

it 'returns status code 201' do
expect(response).to have_http_status(201)
end
{ email: '[email protected]', password_digest: 'password1', username: 'user11', mentor: true, mentee: true}
end
before { post '/api/v1/api_users', params: valid_attributes, as: :json}
context 'when request is valid' do
it 'returns status code 201' do
expect(response).to have_http_status(201)
end

it 'returns same params as entered' do
expect(json['email'], json['password_digest']).to eq('[email protected]','password1', 'user11')
end
it 'returns same params as entered' do
expect([json["email"], json["password_digest"], json["username"]]).to eq(["[email protected]", "password1", "user11"])
end
end

context 'when the request is invalid as no params' do
let(:invalid_attributes) do
{ email: nil } .to_json
before { post '/api/v1/api_users', params: invalid_attributes }

context 'when the request is invalid as no params' do
invalid_attributes = { }
before { post '/api/v1/api_users', params: invalid_attributes, as: :json }
it 'returns status code 422' do
expect(response).to have_http_status(422)
end
Expand All @@ -82,134 +78,113 @@
expect(json['message'])
.to match(/param is missing or the value is empty: api_user/)
end
end
end

context 'when the request is invalid as email already in use' do
let(:invalid_email) do
{ 'email': '[email protected]', 'password_digest': 'password2', 'username': 'user12' } .to_json
before { post '/api/v1/api_users', params: invalid_email }
let(:invalid_email) do
{ 'email': '[email protected]', 'password_digest': 'password2', 'username': 'user12', mentor: true, mentee: true }
end
before { post '/api/v1/api_users', params: invalid_email, as: :json }

it 'returns status code 422' do
expect(response).to have_http_status(422)
expect(response).to have_http_status(422)
end

it 'returns a validation failure message' do
expect(json['message'])
.to match(/Validation failed: Email has already been taken/)
end
end
end

context 'when the request is invalid as username already in use' do
let(:invalid_username) do
{ 'email': '[email protected]', 'password_digest': 'password2', 'username': 'user11' } .to_json
before { post '/api/v1/api_users', params: invalid_username }
let(:invalid_username) do
{ 'email': '[email protected]', 'password_digest': 'password2', 'username': 'user11' , mentor: true, mentee: true}
end
before { post '/api/v1/api_users', params: invalid_username, as: :json }

it 'returns status code 422' do
expect(response).to have_http_status(422)
end
it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns a validation failure message' do
expect(json['message'])
.to match(/Validation failed: Username has already been taken/)
end
it 'returns a validation failure message' do
expect(json['message'])
.to match(/Validation failed: Username has already been taken/)
end
end

context 'when the request is invalid as only some requird params' do
let(:invalid_attributes) { { 'api_user': { 'email': '[email protected]' } }.to_json }
before { post '/api/v1/api_users', params: invalid_attributes }
let(:some_invalid_attributes) {{ 'email': '[email protected]' }}
before { post '/api/v1/api_users', params: some_invalid_attributes, as: :json }

it 'returns status code 422' do
expect(response).to have_http_status(422)
end
it 'returns a validation failure message' do
expect(json['message'])
.to match(/param is missing or the value is empty: api_user/)
.to match(/Validation failed: Password digest can't be blank, Username can't be blank, Mentor can't be blank, Mentee can't be blank/)
end
end

end

# Test suite for Patch /api/v1/api_userss/:id
describe 'PATCH /api/v1/api_users/:id' do
let(:valid_attributes) do
# send json payload
{ 'first_name': 'Bobby','last_name': 'Dylan', 'city': 'Mexico', 'email': '[email protected]', 'password_digest': 'password1', 'mentor': True, 'mentee': False}.to_json
let(:api_user_id) {api_users.first.id}

context 'when request is valid' do
before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes}
let!(:api_user_id) {api_users.first.id}
let(:valid_attributes) do
{ first_name: 'Bobby',last_name: 'Dylan', city: 'Mexico', email: '[email protected]', username: 'user13', password_digest: 'password1' }
end

it 'returns status code 204' do
expect(response).to have_http_status(204)
end
end

context 'check that parameters have updated correctly' do
before { get "/api/v1/api_users/#{api_user_id}/", params: valid_attributes}
end

it 'returns same params as entered' do
expect(json['first_name'],json['last_name'],json['city'], json['email'], json['password_digest'],json['mentor'],json['mentee']).to eq('Bobby', 'Dylan','Mexico', '[email protected]','password1', True, False)
end
end



context 'when email already in use' do
let(:api_user_id) {api_users.first.id}
let(:invalid_email_update) do
# send json payload
{ 'email': '[email protected]'}.to_json
before { patch "/api/v1/api_users/#{api_user_id}/", params: invalid_email_updates}

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns message informing no user with that id' do
expect(json['message']).to match(/Validation failed: Email has already been taken/)
end
end
end

context 'when username already in use' do
let(:api_user_id) {api_users.first.id}
let(:invalid_username_update) do
# send json payload
{ 'username': 'user11'}.to_json
before { patch "/api/v1/api_users/#{api_user_id}/", params: invalid_username_updates}

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns message informing no user with that id' do
expect(json['message']).to match(/Validation failed: Username has already been taken/)
end
end
end



context 'when api_user does not exist' do
let(:api_user_id) {0}
let(:valid_attributes) do
# send json payload
{ 'email': '[email protected]', 'password_digest': 'password1'}.to_json
before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes}

it 'returns status code 404' do
expect(response).to have_http_status(404)
end

it 'returns message informing no user with that id' do
expect(json['message']).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/)
end
end
end

before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes, as: :json}
it 'returns status code 204' do
expect(response).to have_http_status(204)
end

context 'check update worked' do
before { get "/api/v1/api_users/#{api_user_id}/"}
it 'returns same params as entered' do

expect([json["first_name"], json["last_name"], json["city"], json["email"], json["password_digest"], json["username"]]).to eq(['Bobby', 'Dylan','Mexico', '[email protected]','password1','user13'])
end
end

# context 'when email already in use' do
# let(:api_users.first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave these here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have been removed


context 'invalid email' do
before { patch "/api/v1/api_users/#{api_user_id}/", params: {email: api_users.second.email} , as: :json}

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns message informing no user with that id' do
expect(json["message"]).to match(/Validation failed: Email has already been taken/)
end
end

context 'when username already in use' do
before { patch "/api/v1/api_users/#{api_user_id}/", params: {username: api_users.second.username}, as: :json}

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns message informing no user with that id' do
expect(json["message"]).to match(/Validation failed: Username has already been taken/)
end
end

context 'when api_user does not exist' do
let(:api_user_id) {0}
before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes, as: :json}
it 'returns status code 404' do
expect(response).to have_http_status(404)
end

it 'returns message informing no user with that id' do
expect(json['message']).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/)
end
end
end


Expand Down
Loading