Commit 386b5a4d authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei

Added occupations structure and testing

Signed-off-by: Bruno Freitas Tissei's avatarBruno Freitas Tissei <bft15@inf.ufpr.br>
parent e3589acc
Pipeline #6792 passed with stage
in 3 minutes and 18 seconds
GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.0.1)
actionpack (= 5.0.0.1)
nio4r (~> 1.2)
websocket-driver (~> 0.6.1)
actionmailer (5.0.0.1)
actionpack (= 5.0.0.1)
actionview (= 5.0.0.1)
activejob (= 5.0.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.0.1)
actionview (= 5.0.0.1)
activesupport (= 5.0.0.1)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.0.1)
activesupport (= 5.0.0.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
active_model_serializers (0.10.2)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
jsonapi (~> 0.1.1.beta2)
railties (>= 4.1, < 6)
activejob (5.0.0.1)
activesupport (= 5.0.0.1)
globalid (>= 0.3.6)
activemodel (5.0.0.1)
activesupport (= 5.0.0.1)
activerecord (5.0.0.1)
activemodel (= 5.0.0.1)
activesupport (= 5.0.0.1)
arel (~> 7.0)
activesupport (5.0.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.1)
bcrypt (3.1.11)
builder (3.2.2)
byebug (9.0.5)
coderay (1.1.1)
concurrent-ruby (1.0.2)
devise (4.2.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0, < 5.1)
responders
warden (~> 1.2.3)
devise_token_auth (0.1.39)
devise (> 3.5.2, <= 4.2)
rails (< 6)
erubis (2.7.0)
ffi (1.9.14)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
json (1.8.3)
jsonapi (0.1.1.beta2)
json (~> 1.8)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.0)
minitest-rails (3.0.0)
minitest (~> 5.8)
railties (~> 5.0)
nio4r (1.2.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
orm_adapter (0.5.0)
pg (0.18.4)
pkg-config (1.1.7)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
puma (3.6.0)
pundit (1.1.0)
activesupport (>= 3.0.0)
rack (2.0.1)
rack-attack (5.0.1)
rack
rack-cors (0.4.0)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.0.1)
actioncable (= 5.0.0.1)
actionmailer (= 5.0.0.1)
actionpack (= 5.0.0.1)
actionview (= 5.0.0.1)
activejob (= 5.0.0.1)
activemodel (= 5.0.0.1)
activerecord (= 5.0.0.1)
activesupport (= 5.0.0.1)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0.1)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.1)
actionpack (~> 5.x)
actionview (~> 5.x)
activesupport (~> 5.x)
rails-dom-testing (2.0.1)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6.0)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.0.1)
actionpack (= 5.0.0.1)
activesupport (= 5.0.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.2.2)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
slop (3.6.0)
spring (1.7.2)
spring-watcher-listen (2.0.0)
listen (>= 2.7, < 4.0)
spring (~> 1.2)
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.1.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.1)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
warden (1.2.6)
rack (>= 1.0)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
PLATFORMS
ruby
DEPENDENCIES
active_model_serializers
byebug
devise
devise_token_auth
listen (~> 3.0.5)
minitest-rails
pg (~> 0.18)
pry-rails
puma (~> 3.0)
pundit
rack-attack
rack-cors
rails (~> 5.0.0)
rails-controller-testing
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data
BUNDLED WITH
1.12.5
......@@ -4,7 +4,7 @@ module Api::V1
# GET /citizens
def index
@citizens = Citizen.where(active: true)
@citizens = Citizen.all_active
render json: @citizens
end
......
module Api::V1
class OccupationsController < ApplicationController
before_action :set_occupation, only: [:show, :update, :destroy]
# GET /occupations
def index
@occupations = Occupation.all_active
render json: @occupations
end
# GET /occupations/1
def show
if @occupation.nil?
render json: {
errors: ["Occupation #{params[:id]} does not exist."]
}, status: 400
else
render json: @occupation
end
end
# POST /occupations
def create
@occupation = Occupation.new(occupation_params)
@occupation.active = true
if @occupation.save
render json: @occupation, status: :created
else
render json: @occupation.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /occupations/1
def update
if @occupation.nil?
render json: {
errors: ["Occupation #{params[:id]} does not exist."]
}, status: 400
else
if @occupation.update(occupation_params)
render json: @occupation
else
render json: @occupation.errors, status: :unprocessable_entity
end
end
end
# DELETE /occupations/1
def destroy
if @occupation.nil?
render json: {
errors: ["Occupation #{params[:id]} does not exist."]
}, status: 400
else
@occupation.active = false
@occupation.save!
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_occupation
begin
@occupation = Occupation.find(params[:id])
rescue
@occupation = nil
end
end
# Only allow a trusted parameter "white list" through.
def occupation_params
params.require(:occupation).permit(:description, :name,
:active, :city_hall_id)
end
end
end
class Occupation < ApplicationRecord
# Associations #
has_many :professionals
belongs_to :city_hall
# Validations #
validates_presence_of :name, :description, :city_hall_id
validates_inclusion_of :active, :in => [true,false]
validates_format_of :name, :with => /\A[^0-9`!@#\$%\^&*+_=]+\z/
# @return all active occupations
def self.all_active
Occupation.where(active: true)
end
end
class Professional < ApplicationRecord
# Associations #
belongs_to :account
belongs_to :occupation
has_one :citizen, through: :account
has_many :professionals_service_places
has_many :service_places, :through => :professionals_service_places
# Validations #
validates_presence_of :occupation_id, :account_id
# @return list of professional's columns
def self.keys
return [ :active, :registration ]
......
class OccupationSerializer < ActiveModel::Serializer
attributes :id, :description, :name, :active
has_one :city_hall
end
......@@ -10,6 +10,7 @@ Rails.application.routes.draw do
resources :city_halls
resources :professionals
resources :service_places
resources :occupations
end
end
end
class CreateOccupations < ActiveRecord::Migration[5.0]
def change
create_table :occupations do |t|
t.string :description
t.string :name
t.boolean :active
t.references :city_hall, foreign_key: true
t.timestamps
end
end
end
class AddOccupationRefToProfessionals < ActiveRecord::Migration[5.0]
def change
add_reference :professionals, :occupation, foreign_key: true
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160826131632) do
ActiveRecord::Schema.define(version: 20160906123049) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -34,6 +34,11 @@ ActiveRecord::Schema.define(version: 20160826131632) do
t.index ["uid"], name: "index_accounts_on_uid", unique: true, using: :btree
end
create_table "accounts_service_places", id: false, force: :cascade do |t|
t.integer "account_id", null: false
t.integer "service_place_id", null: false
end
create_table "cities", force: :cascade do |t|
t.string "ibge_code", null: false
t.string "name", null: false
......@@ -70,6 +75,7 @@ ActiveRecord::Schema.define(version: 20160826131632) do
end
create_table "city_halls", force: :cascade do |t|
t.integer "city_id"
t.boolean "active"
t.string "address_number", limit: 10, null: false
t.string "address_street", null: false
......@@ -95,8 +101,52 @@ ActiveRecord::Schema.define(version: 20160826131632) do
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "city_id"
t.index ["city_id"], name: "index_city_halls_on_city_id", using: :btree
end
create_table "occupations", force: :cascade do |t|
t.string "description"
t.string "name"
t.boolean "active"
t.integer "city_hall_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["city_hall_id"], name: "index_occupations_on_city_hall_id", using: :btree
end
create_table "professionals", force: :cascade do |t|
t.string "registration"
t.boolean "active", default: true, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "account_id"
t.integer "occupation_id"
t.index ["account_id"], name: "index_professionals_on_account_id", using: :btree
t.index ["occupation_id"], name: "index_professionals_on_occupation_id", using: :btree
end
create_table "professionals_service_places", id: false, force: :cascade do |t|
t.integer "professional_id", null: false
t.integer "service_place_id", null: false
t.string "role", null: false
t.boolean "active", default: true, null: false
end
create_table "service_places", force: :cascade do |t|
t.string "name", null: false
t.string "cep", limit: 10
t.string "neighborhood", null: false
t.string "address_street", null: false
t.string "address_number", limit: 10, null: false
t.string "address_complement"
t.string "phone1", limit: 13
t.string "phone2", limit: 13
t.string "email"
t.string "url"
t.boolean "active", default: true, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "city_hall_id"
t.index ["city_hall_id"], name: "index_service_places_on_city_hall_id", using: :btree
end
create_table "states", force: :cascade do |t|
......@@ -109,5 +159,6 @@ ActiveRecord::Schema.define(version: 20160826131632) do
add_foreign_key "cities", "states"
add_foreign_key "citizens", "accounts"
add_foreign_key "city_halls", "cities"
end
\ No newline at end of file
add_foreign_key "occupations", "city_halls"
add_foreign_key "professionals", "occupations"
end
require 'test_helper'
class OccupationsControllerTest < ActionDispatch::IntegrationTest
describe "Token access" do
before do
@citizen = Citizen.new(cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
rg: "1234567")
@account = Account.new(uid: @citizen.cpf,
password: "123mudar",
password_confirmation: "123mudar")
@parana = State.new(abbreviation: "PR",
ibge_code: "41",
name: "Paraná")
@parana.save!
@curitiba = City.new(ibge_code: "4106902",
name: "Curitiba",
state_id: @parana.id)
@curitiba.save!
@curitiba_city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
city_id: @curitiba.id,
phone1: "1414141414",
active: true,
block_text: "Test block text");
@citizen.active = true
@account.save!
@citizen.account_id = @account.id
@citizen.save!
@curitiba_city_hall.save!
@auth_headers = @account.create_new_auth_token
@token = @auth_headers['access-token']
@client_id = @auth_headers['client']
@expiry = @auth_headers['expiry']
end
describe "Successful request to create occupation" do
before do
@number_of_occupations = Occupation.count
post '/v1/occupations', params: {occupation: {
description: "Teste",
name: "Tester",
active: true,
city_hall_id: @curitiba_city_hall.id
}}, headers: @auth_headers
@body = JSON.parse(response.body)
@resp_token = response.headers['access-token']
@resp_client_id = response.headers['client']
@resp_expiry = response.headers['expiry']
@resp_uid = response.headers['uid']
end
it "should be successful" do
assert_equal 201, response.status
end
it "should correspond to the created occupation" do
assert_equal "Teste", @body["description"]
end
it "should create a occupation" do
assert_equal @number_of_occupations + 1, Occupation.count
end
describe "Successful request to show all occupations" do
before do
get '/v1/occupations', params: {},
headers: @auth_headers
@body = JSON.parse(response.body)
@resp_token = response.headers['access-token']
@resp_client_id = response.headers['client']
@resp_expiry = response.headers['expiry']
@resp_uid = response.headers['uid']
end
it "should be successful" do
assert_equal 200, response.status
end
it "should return every city hall" do
assert_equal Occupation.count, @body.size
end
end
end
describe "Unsuccessful request to show occupation that doesn't exists" do
before do
get '/v1/occupations/222', params: {},
headers: @auth_headers
@body = JSON.parse(response.body)
@resp_token = response.headers['access-token']
@resp_client_id = response.headers['client']
@resp_expiry = response.headers['expiry']
@resp_uid = response.headers['uid']
end
it "should not be successful" do
assert_equal 400, response.status
end
it "should return an error message" do
assert_not_empty @body['errors']
end
end
describe "Unsuccessful request to create occupation with missing city_hall" do
before do
@number_of_occupations = Occupation.count
post '/v1/occupations', params: {occupation: {
description: "Teste",
name: "Tester",
active: true,
}}, headers: @auth_headers
@body = JSON.parse(response.body)
@resp_token = response.headers['access-token']
@resp_client_id = response.headers['client']
@resp_expiry = response.headers['expiry']
@resp_uid = response.headers['uid']
end
it "should be unsuccessful" do
assert_equal 422, response.status
end
it "should not create any occupation" do
assert_equal @number_of_occupations, Occupation.count
end
end
describe "Unsuccessful request to delete occupation that doesn't exists" do
before do
@number_of_occupations = Occupation.all_active.count
delete '/v1/occupations/222', params: {},
headers: @auth_headers
@body = JSON.parse(response.body)
@resp_token = response.headers['access-token']
@resp_client_id = response.headers['client']
@resp_expiry = response.headers['expiry']
@resp_uid = response.headers['uid']
end
it "should not be successful" do
assert_equal 400, response.status
end
it "should return an error message" do
assert_not_empty @body['errors']
end
test "number of occupations should not be decreased" do
assert_equal @number_of_occupations, Occupation.all_active.count
end
end
end
end
......@@ -10,11 +10,42 @@ class Api::V1::ProfessionalsControllerTest < ActionDispatch::IntegrationTest
name: "Test Example",
phone1: "(12)1212-1212",
rg: "1234567")
@account = Account.new(uid: @citizen.cpf,
password: "123mudar",
password_confirmation: "123mudar")
@parana = State.new(abbreviation: "PR",
ibge_code: "41",
name: "Paraná")
@parana.save!
@curitiba = City.new(ibge_code: "4106902",
name: "Curitiba",
state_id: @parana.id)
@curitiba.save!
@curitiba_city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
city_id: @curitiba.id,
phone1: "1414141414",
active: true,
block_text: "Test block text");
@curitiba_city_hall.save!
@occupation = Occupation.new(description: "Teste",
name: "Tester",
active: true,
city_hall_id: @curitiba_city_hall.id)
@occupation.save!
@professional = Professional.new(active: true,
registration: "123")
registration: "123",
occupation_id: @occupation.id)
@account.save!
@citizen.account_id = @account.id
@citizen.save!
......
......@@ -16,8 +16,6 @@ class Api::V1::ServicePlacesControllerTest < ActionDispatch::IntegrationTest
password: "123mudar",
password_confirmation: "123mudar")
@professional = Professional.new(active: true,
registration: "123")
@parana = State.new(abbreviation: "PR",
ibge_code: "41",
......@@ -42,10 +40,18 @@ class Api::V1::ServicePlacesControllerTest < ActionDispatch::IntegrationTest
@account.save!
@citizen.account_id = @account.id
@citizen.save!
@professional.account_id = @account.id
@professional.save!
@city_hall.save!
@occupation = Occupation.new(description: "Teste",
name: "Tester",
active: true,
city_hall_id: @city_hall.id)
@occupation.save!
@professional = Professional.new(active: true,
registration: "123",
occupation_id: @occupation.id)
@professional.account_id = @account.id
@professional.save!
@service_place = ServicePlace.new(active: true,
address_number: "123",
......
require "test_helper"
class OccupationTest < ActiveSupport::TestCase
describe Occupation do
before do
@parana = State.new(abbreviation: "PR",
ibge_code: "41",
name: "Paraná")
@parana.save!
@curitiba = City.new(name: "Curitiba",
ibge_code: "4106902",
state_id: @parana.id)
@curitiba.save!
@curitiba_city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
city_id: @curitiba.id,
phone1: "1414141414",
active: true,
block_text: "Test block text")
@curitiba_city_hall.save!
end
describe "missing name " do
before do
@occupation = Occupation.new(description: "Teste",
active: true,
city_hall_id: @curitiba_city_hall.id)
end
it "should return an error" do
@occupation.save
assert_not_empty @occupation.errors.messages[:name]
end
it "should not save" do
assert_not @occupation.save
end
end
describe "missing description id" do
before do
@occupation = Occupation.new(name: "Tester",
active: true,
city_hall_id: @curitiba_city_hall.id)
end
it "should return an error" do
@occupation.save
assert_not_empty @occupation.errors.messages[:description]
end