professionals_controller_test.rb 3.76 KB
Newer Older
Rafael Ravedutti's avatar
Rafael Ravedutti committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# This file is part of Agendador.
#
# Agendador is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Agendador is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Agendador.  If not, see <https://www.gnu.org/licenses/>.

Mateus Ravedutti's avatar
Mateus Ravedutti committed
16 17
require 'test_helper'

18 19 20
class Api::V1::ProfessionalsControllerTest < ActionDispatch::IntegrationTest
  describe "Token access" do
    before do
21 22 23 24 25
      @parana = State.new(
        abbreviation: "PR",
        ibge_code: "41",
        name: "Paraná"
      )
mateus ravedutti's avatar
mateus ravedutti committed
26 27
      @parana.save!

28 29 30 31 32
      @curitiba = City.new(
        ibge_code: "4106902",
        name: "Curitiba",
        state_id: @parana.id
      )
mateus ravedutti's avatar
mateus ravedutti committed
33 34
      @curitiba.save!

35 36
      @citizen= Citizen.new(
        active: true,
37 38
        cpf: "10845922904",
        birth_date: "18/04/1997",
39
        cep: "81530110", 
40
        email: "test@example.com",
41
        name: "Test Example",
42 43
        phone1: "(12)1212-1212",
        rg: "1234567",
44 45
        address_street: "Street from Curitiba",
        address_number: "4121",
46 47
        city_id: @curitiba.id
      )
48

49 50 51 52 53 54 55 56
      @account = Account.new(
        uid: @citizen.cpf,
        password: "123mudar",
        password_confirmation: "123mudar"
      )
      @account.save!
      @citizen.account_id = @account.id
      @citizen.save!
57

58 59
      @curitiba_city_hall = CityHall.new(
        name: "Prefeitura de Curitiba",
60
        cep: "81530110",
61 62 63 64 65 66 67
        neighborhood: "Test neighborhood",
        address_street: "Test street",
        address_number: "123",
        phone1: "1414141414",
        active: true,
        block_text: "Test block text"
      )
68 69
      @curitiba_city_hall.save!

70 71 72 73 74 75
      @occupation = Occupation.new(
        description: "Teste",
        name: "Tester",
        active: true,
        city_hall_id: @curitiba_city_hall.id
      )
76 77
      @occupation.save!

78
      @auth_headers = @account.create_new_auth_token
Mateus Ravedutti's avatar
Mateus Ravedutti committed
79

80 81 82
      @token     = @auth_headers['access-token']
      @client_id = @auth_headers['client']
      @expiry    = @auth_headers['expiry']
Mateus Ravedutti's avatar
Mateus Ravedutti committed
83 84
    end

85
    describe "Unsuccessful request to create professional" do
86
      before do
mateus ravedutti's avatar
mateus ravedutti committed
87 88 89 90 91 92 93
        @number_of_professionals = Professional.count

        post '/v1/professionals', params: {professional: {
          active: true,
          registration: "123",
          occupation_id: @occupation.id,
          account_id: @account.id
94
        }, permission: "citizen"}, headers: @auth_headers
Mateus Ravedutti's avatar
Mateus Ravedutti committed
95

96 97 98 99 100
        @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']
mateus ravedutti's avatar
mateus ravedutti committed
101
      end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
102

103
      it "should not be permitted" do
104
        assert_equal 422, response.status
105 106
      end

107 108
      it "should not create a professional" do
        assert_equal @number_of_professionals, Professional.count
109 110
      end

111
      describe "Unsuccessful request to show all professionals" do
112 113
        before do
          get '/v1/professionals', params: {permission: "citizen"},
114
            headers: @auth_headers
Mateus Ravedutti's avatar
Mateus Ravedutti committed
115

mateus ravedutti's avatar
mateus ravedutti committed
116 117 118 119 120 121
          @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
122

123 124
        it "should not be permitted" do
          assert_equal 403, response.status
mateus ravedutti's avatar
mateus ravedutti committed
125
        end
126 127
      end
    end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
128 129
  end
end