dependants_controller_test.rb 6.15 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 18 19 20
require 'test_helper'

class Api::V1::DependantsControllerTest < ActionDispatch::IntegrationTest
  describe "Token access" do
    before do
21 22 23 24 25
      @santa_catarina = State.new(
        abbreviation: "SC",
        ibge_code: "42",
        name: "Santa Catarina"
      )
mateus ravedutti's avatar
mateus ravedutti committed
26 27
      @santa_catarina.save!

28 29 30 31 32
      @joinville = City.new(
        ibge_code: "4209102",
        name: "Joinville",
        state_id: @santa_catarina.id
      )
mateus ravedutti's avatar
mateus ravedutti committed
33 34
      @joinville.save!

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

      @account = Account.new(
50
        uid: @responsible.cpf,
51 52 53
        password: "123mudar",
        password_confirmation: "123mudar"
      )
54

Mateus Ravedutti's avatar
Mateus Ravedutti committed
55
      @account.save!
56 57
      @responsible.account_id = @account.id
      @responsible.save!
Mateus Ravedutti's avatar
Mateus Ravedutti committed
58 59 60 61 62 63 64 65

      @auth_headers = @account.create_new_auth_token

      @token     = @auth_headers['access-token']
      @client_id = @auth_headers['client']
      @expiry    = @auth_headers['expiry']
    end

mateus ravedutti's avatar
mateus ravedutti committed
66
    describe "Successful request to create dependant" do
67
      before do
mateus ravedutti's avatar
mateus ravedutti committed
68 69
        @number_of_dependants = Dependant.count

70 71 72
        post '/v1/citizens/' + @responsible.id.to_s + '/dependants', params: {
          dependant: {
            active: true,
73 74 75
            cpf: "18472377628",
            birth_date: "16/03/2004",
            cep: "89218230",
76
            email: "test@example.com",
77
            name: "Test Example Dep",
78 79
            phone1: "(12)1212-1212",
            city_id: @joinville.id,
Rafael Ravedutti's avatar
Rafael Ravedutti committed
80
            address_number: 123456,
81
            rg: "1234567"
82
          }, permission: "citizen"
83
        }, headers: @auth_headers
Mateus Ravedutti's avatar
Mateus Ravedutti committed
84 85 86 87 88 89

        @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']
90
      end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
91 92

      it "should be successful" do
mateus ravedutti's avatar
mateus ravedutti committed
93
        assert_equal 201, response.status
Mateus Ravedutti's avatar
Mateus Ravedutti committed
94 95
      end

mateus ravedutti's avatar
mateus ravedutti committed
96
      it "should correspond to the created dependant" do
97 98 99 100
          @dependant = Dependant.where(citizens: {
            responsible_id: @responsible.id
          }).includes(:citizen).first
        assert_equal @dependant.citizen.id, @body["citizen"]["id"]
Mateus Ravedutti's avatar
Mateus Ravedutti committed
101 102
      end

mateus ravedutti's avatar
mateus ravedutti committed
103
      it "should create a dependant" do
104
        assert_equal @number_of_dependants + 1, Dependant.count
Mateus Ravedutti's avatar
Mateus Ravedutti committed
105 106
      end

mateus ravedutti's avatar
mateus ravedutti committed
107
      describe "Successful request to show dependant" do
108
        before do
109 110 111
          @dependant = Dependant.where(citizens: {
            responsible_id: @responsible.id
          }).includes(:citizen).first
112

113 114
          get '/v1/citizens/' + @responsible.id.to_s + '/dependants/' + @dependant.id.to_s,
            params: {permission: "citizen"},
115
            headers: @auth_headers
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']
122
        end
mateus ravedutti's avatar
mateus ravedutti committed
123 124 125 126 127 128

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

        it "should correspond to the current account" do
129 130
          assert_equal Citizen.find(@body["citizen"]["id"]).responsible_id, @controller
            .current_account.citizen.id
mateus ravedutti's avatar
mateus ravedutti committed
131 132 133 134 135
        end

        it "should correspond to the dependant in the database" do
          assert_equal @body["citizen"]["cpf"], Dependant.find(@dependant.id).citizen.cpf
        end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
136 137
      end

mateus ravedutti's avatar
mateus ravedutti committed
138 139
      describe "Successful request to delete dependant" do
        before do
140 141 142
          @dependant = Dependant.where(citizens: {
            responsible_id: @responsible.id
          }).includes(:citizen).first
mateus ravedutti's avatar
mateus ravedutti committed
143

144 145
          delete '/v1/citizens/' + @responsible.id.to_s + '/dependants/' + @dependant.id.to_s,
            params: {permission: "citizen"},
146
            headers: @auth_headers
mateus ravedutti's avatar
mateus ravedutti committed
147 148 149 150 151 152

          @resp_token = response.headers['access-token']
          @resp_client_id = response.headers['client']
          @resp_expiry = response.headers['expiry']
          @resp_uid = response.headers['uid']
        end
153

mateus ravedutti's avatar
mateus ravedutti committed
154 155 156 157 158
        it "should be successful" do
          assert_equal 204, response.status
        end

        it "should have been deleted" do
159
          assert_equal Dependant.where(id: @dependant.id).first.citizen.active, false
mateus ravedutti's avatar
mateus ravedutti committed
160
        end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
161 162
      end

mateus ravedutti's avatar
mateus ravedutti committed
163 164
      describe "Successful request to update dependant" do
        before do
165 166 167
          @dependant = Dependant.where(citizens: {
            responsible_id: @responsible.id
          }).includes(:citizen).first
Mateus Ravedutti's avatar
Mateus Ravedutti committed
168

169
          put '/v1/citizens/' + @responsible.id.to_s + '/dependants/' + @dependant.id.to_s,
170
            params: {dependant: { phone1: "(41)13131313" }, permission: "citizen"},
171
            headers: @auth_headers
172

mateus ravedutti's avatar
mateus ravedutti committed
173 174 175 176 177 178 179 180 181
          @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
Mateus Ravedutti's avatar
Mateus Ravedutti committed
182

183 184 185 186 187 188
        test "phone number should have been changed" do
          @dependant = Dependant.where(citizens: {
            responsible_id: @responsible.id
          }).includes(:citizen).first

          assert_equal "(41)13131313", @dependant.citizen.phone1
mateus ravedutti's avatar
mateus ravedutti committed
189
        end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
190 191 192 193
      end
    end
  end
end