Skip to content

Commit

Permalink
Merge pull request #282 from fga-eps-mds/feature/266-user-already-reg…
Browse files Browse the repository at this point in the history
…istered-in-system

#266 - Verify the user token
  • Loading branch information
min-ia committed Nov 5, 2018
2 parents ae8a3a5 + 4daaf05 commit 5fececa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
11 changes: 10 additions & 1 deletion indicaAi/app/models/user_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ class UserIdentifier < ApplicationRecord
has_many :local_ratings

validates :identifier, presence: true
validates_numericality_of :identifier
validates_uniqueness_of :identifier

def self.find_favorites(params)
UserIdentifier.find(params).favorite_locals
end

def self.get_by_token(token)
# TODO: Validade token with api
user = UserIdentifier.find_by(identifier: token)
if user.nil?
UserIdentifier.create(identifier: token)
else
user
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeUserIdentifierToString < ActiveRecord::Migration[5.1]
def change
change_column :user_identifiers, :identifier, :string
end
end
11 changes: 5 additions & 6 deletions indicaAi/spec/controllers/user_identifiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@
end

RSpec.describe UserIdentifiersController, type: :controller do
let!(:users_test) { create_list(:user_identifier, 10) }
let!(:user) { users_test.first }
let!(:users_test) { create(:user_identifier) }
let!(:favorites_test) do
create_list(:favorite_local, 10, user_identifier: user)
create_list(:favorite_local, 10, user_identifier: users_test)
end
describe 'GET list_favorites' do
it 'should returns list of favorites to assign @favorites ' do
get :list_favorites, params: { id: user.id }
get :list_favorites, params: { id: users_test.id }
should route(
:get, "/users/#{user.id}/favorites"
:get, "/users/#{users_test.id}/favorites"
).to(
action: :list_favorites, id: user.id
action: :list_favorites, id: users_test.id
)
expect(response).to be_success
assert assigns(:favorites) == favorites_test
Expand Down
2 changes: 1 addition & 1 deletion indicaAi/spec/factories/user_identifier.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :user_identifier do
identifier { Faker::Number.unique.between(1, 9999) }
identifier { Faker::Lorem.unique.word }
end
end
28 changes: 18 additions & 10 deletions indicaAi/spec/models/user_identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@
context 'Validate of UserIdentifier' do
it { should validate_presence_of(:identifier) }
it { should have_many(:favorite_locals).dependent(:destroy) }
it { should validate_numericality_of(:identifier) }
it { should validate_uniqueness_of(:identifier) }
it 'user valid ' do
user = create(:user_identifier)
expect(user.valid?).to be_truthy
end

it 'user invalid - without identifier or is not number' do
# identifier empty
user = UserIdentifier.new
user.save
expect(user.valid?).to be_falsey
user.identifier = Faker::Lorem.word
expect(user.valid?).to be_falsey
end

it 'user invalid - is not unique' do
number = Faker::Number.between(1, 9999)
user = UserIdentifier.new(identifier: number)
Expand All @@ -31,6 +21,24 @@
end
end

RSpec.describe UserIdentifier, type: :model do
context 'Get UserIdentifier by token' do
let!(:user) { create(:user_identifier) }

it 'should return user' do
token = user.identifier
result = UserIdentifier.get_by_token(token)
assert result, user
end
it 'should return new user' do
count = UserIdentifier.count
token = 'dsfoanoi1ni3no2n3fo23n'
UserIdentifier.get_by_token(token)
assert count, (count + 1)
end
end
end

RSpec.describe UserIdentifier, type: :model do
let!(:user) { create(:user_identifier) }
let!(:user2) { create(:user_identifier) }
Expand Down

0 comments on commit 5fececa

Please sign in to comment.