File: respond_with_users_controller_spec.rb

package info (click to toggle)
ruby-acts-as-api 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 624 kB
  • sloc: ruby: 2,366; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 3,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'spec_helper'

describe SharedEngine::RespondWithUsersController, type: :controller do
  routes { SharedEngine::Engine.routes }

  before(:each) do
    setup_models
  end

  after(:each) do
    clean_up_models
  end

  # see spec/support/controller_examples.rb
  it_behaves_like 'a controller with ActsAsApi responses'

  describe 'default ActionController::Responder behavior' do
    context 'json responses' do
      context 'creating valid models' do
        before(:each) do
          post :create, format: 'json', params: { user: { first_name: 'Luke', last_name: 'Skywalker' }, api_template: :name_only }
        end

        it 'should return HTTP 201 status' do
          expect(response.code).to eq('201')
        end

        it 'should contain the specified attributes' do
          expect(response_body_json['user']).to have_key('first_name')
          expect(response_body_json['user']).to have_key('last_name')
        end

        it 'should contain the specified values' do
          expect(response_body_json['user']['first_name']).to eql('Luke')
          expect(response_body_json['user']['last_name']).to eql('Skywalker')
        end
      end

      context 'creating invalid models' do
        before(:each) do
          post :create, format: 'json', params: { user: { first_name: 'Luke' }, api_template: :name_only }
        end

        it 'should return HTTP 422 status' do
          expect(response.code).to eq('422')
        end

        it 'should return errors as json' do
          expect(response_body_json['errors']['last_name']).to include('can\'t be blank')
        end
      end

      context 'returning all models without default root and no order' do
        before(:each) do
          get :index_no_root_no_order, format: 'json', params: { api_template: :name_only }
        end

        it 'should return HTTP 200 status' do
          expect(response.code).to eq('200')
        end

        it 'should contain the specified attributes' do
          response_body_json['users'].each do |user|
            expect(user).to have_key('first_name')
            expect(user).to have_key('last_name')
          end
        end
      end
    end

    context 'xml responses' do
      context 'creating valid models' do
        before(:each) do
          post :create, format: 'xml', params: { user: { first_name: 'Luke', last_name: 'Skywalker' }, api_template: :name_only }
        end

        it 'should return HTTP 201 status' do
          expect(response.code).to eq('201')
        end

        it 'should include HTTP Location header' do
          expect(response.headers['Location']).to match "/shared/users/#{User.last.id}"
        end

        it 'should contain the specified attributes' do
          expect(response_body).to have_selector('user > first-name')
          expect(response_body).to have_selector('user > last-name')
        end
      end

      context 'creating invalid models' do
        before(:each) do
          post :create, format: 'xml', params: { user: { first_name: 'Luke' }, api_template: :name_only }
        end

        it 'should return HTTP 422 status' do
          expect(response.code).to eq('422')
        end

        it 'should return errors as xml' do
          expect(response_body).to have_selector('errors > error')
        end
      end

      context 'returning all models without default root and no order' do
        before(:each) do
          get :index_no_root_no_order, format: 'xml', params: { api_template: :name_only }
        end

        it 'should return HTTP 200 status' do
          expect(response.code).to eq('200')
        end

        it 'should contain the specified attributes' do
          expect(response_body).to have_selector('users > user > first-name', count: 3)
          expect(response_body).to have_selector('users > user > last-name', count: 3)
        end
      end
    end
  end
end