professionals_controller_test.rb 3.44 KB
Newer Older
Mateus Ravedutti's avatar
Mateus Ravedutti committed
1 2
require 'test_helper'

3 4 5
class Api::V1::ProfessionalsControllerTest < ActionDispatch::IntegrationTest
  describe "Token access" do
    before do
6
      @citizen= Citizen.new(cpf: "10845922904", 
7 8 9 10 11 12 13 14 15 16 17 18
                             birth_date: "18/04/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")
      @professional = Professional.new(active: true,
				       registration: "123")
      @account.save!
19 20
      @citizen.account_id = @account.id
      @citizen.save!
21 22
      @professional.account_id = @account.id
      @professional.save!
Mateus Ravedutti's avatar
Mateus Ravedutti committed
23

24
      @auth_headers = @account.create_new_auth_token
Mateus Ravedutti's avatar
Mateus Ravedutti committed
25

26 27 28
      @token     = @auth_headers['access-token']
      @client_id = @auth_headers['client']
      @expiry    = @auth_headers['expiry']
Mateus Ravedutti's avatar
Mateus Ravedutti committed
29 30
    end

31 32 33 34
    describe "Successful request to show professional" do
      before do 
        get '/v1/professionals/' + @professional.id.to_s, params: {}, 
                                                headers: @auth_headers
Mateus Ravedutti's avatar
Mateus Ravedutti committed
35

36 37 38 39 40 41
        @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 
Mateus Ravedutti's avatar
Mateus Ravedutti committed
42

43 44 45 46 47 48 49 50 51
      it "should be successful" do
        assert_equal 200, response.status
      end

      it "should correspond to the current account" do
        assert_equal @body["id"], @controller.current_account.professional.id
      end

      it "should correspond to the professional in the database" do
52
        assert_equal @body["account"]["citizen"]["cpf"], Professional.find(@professional.id).account.citizen.cpf
53 54 55 56 57 58 59
      end
    end

    describe "Successful request to delete professional" do
      before do
        delete '/v1/professionals/' + @professional.id.to_s, params: {}, 
                                                   headers: @auth_headers
Mateus Ravedutti's avatar
Mateus Ravedutti committed
60

61 62 63 64 65 66 67 68 69 70 71 72 73
        @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 204, response.status
      end

      it "should have been deleted" do
        assert_nil Professional.where(id: @professional.id).first
      end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
74 75
    end

76 77 78
    describe "Successful request to update professional" do
      before do
        put '/v1/professionals/' + @professional.id.to_s,
79
                                       params: {professional: {registration: "7654/21" }}, #{professional: {registration: "7654/21"}}, 
80 81 82 83 84 85 86 87 88 89 90
                                       headers: @auth_headers
        @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

91 92 93
      test "registration number should have been changed" do
        @professional = Citizen.where(cpf: @citizen.cpf).first.account.professional
        assert_equal "7654/21", @professional.registration
94 95
      end
    end
Mateus Ravedutti's avatar
Mateus Ravedutti committed
96 97
  end
end