cep_controller_test.rb 2.61 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/>.

16 17 18
require "test_helper"

class Api::V1::CepControllerTest < ActionDispatch::IntegrationTest
19 20
  describe "City creation" do
    before do
21 22 23 24 25
      @parana = State.new(
        abbreviation: "PR",
        ibge_code: "41",
        name: "Paraná"
      )
26 27
      @parana.save!

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

35 36
      @curitiba_city_hall = CityHall.new(
        name: "Prefeitura de Curitiba",
37
        cep: "81530110",
38 39 40 41 42 43 44
        neighborhood: "Test neighborhood",
        address_street: "Test street",
        address_number: "123",
        phone1: "1414141414",
        active: true,
        block_text: "Test block text"
      );
45
      @curitiba_city_hall.save!
46 47
    end

48
    describe "Successful cep validation" do
Rafael Ravedutti's avatar
Rafael Ravedutti committed
49
      before do
50 51 52
        post '/v1/validate_cep', params: {cep: {number: "81530110"}}

        @body = JSON.parse(response.body)
Rafael Ravedutti's avatar
Rafael Ravedutti committed
53
      end
54 55 56 57 58 59 60 61

      it "should be successful" do
        assert_equal 200, response.status
      end

      it "should correspond to actual address" do
        assert_equal "Jardim das Américas", @body["neighborhood"]
      end
62 63
    end

64
    describe "Unsuccessful cep validation that doesn't exist" do
Rafael Ravedutti's avatar
Rafael Ravedutti committed
65 66
      before do
        post '/v1/validate_cep', params: {cep: {number: "815301112"}}
67 68

        @body = JSON.parse(response.body)
Rafael Ravedutti's avatar
Rafael Ravedutti committed
69
      end
70

71 72 73
      it "should be successful" do
        assert_equal 422, response.status
      end
74

75
      it "should return an error" do
76
        assert_equal ["CEP inválido!"], @body["errors"]
77
      end
78
    end
79

80
    describe "Unsuccessful cep validation that is not registered" do
Rafael Ravedutti's avatar
Rafael Ravedutti committed
81
      before do
82 83 84
        post '/v1/validate_cep', params: {cep: {number: "89218230"}}

        @body = JSON.parse(response.body)
Rafael Ravedutti's avatar
Rafael Ravedutti committed
85
      end
86 87 88 89 90 91

      it "should be successful" do
        assert_equal 404, response.status
      end

      it "should return an error" do
92
        assert_equal ["Cidade não registrada!"], @body["errors"]
93
      end
94 95 96
    end
  end
end