Module: Intermodal::RSpec::LinkedResources::ClassMethods

Defined in:
lib/intermodal/rspec/requests/linked_resources.rb

Instance Method Summary collapse

Instance Method Details

#expects_crud_for_linked_resourceObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/intermodal/rspec/requests/linked_resources.rb', line 41

def expects_crud_for_linked_resource
  expects_index :skip_pagination_examples => true do
    it 'should include the parent id' do
      collection.should_not be_empty
      body[resource_element_name.to_s]['id'].should eql(model_parents.first.id)
    end
  end

  expects_list_replace
  expects_list_append
  expects_list_remove
end

#expects_list_append(&blk) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/intermodal/rspec/requests/linked_resources.rb', line 101

def expects_list_append(&blk)
   = 
  request :put, [:collection_url] do
    instance_eval(&blk) if blk
    let(:request_url) { collection_url }
    let(:additional_target_ids) { unlinked_targets.map(&:id) }
    let(:request_payload) { { collection_element_name => additional_target_ids } }
    let(:updated_target_ids) { model.by_parent(model_parent).to_target_ids }

    expects_status(200)
    expects_content_type([:mime_type], [:encoding])

    it 'should link additional targets to manufacturer' do
      model_collection.should_not be_empty
      response.should_not be_empty
      additional_target_ids.each do |additional_target_id|
        updated_target_ids.should include(additional_target_id)
      end
    end

    it 'should append to, but not delete original linked targets' do
      model_collection.should_not be_empty
      response.should_not be_empty
      model_collection.each do |original_target_id|
        updated_target_ids.should include(original_target_id)
      end
    end

    with_malformed_data_should_respond_with_400
    with_nil_target_ids_should_respond_with_422
    expects_unauthorized_access_to_respond_with_401
  end
end

#expects_list_remove(&blk) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/intermodal/rspec/requests/linked_resources.rb', line 135

def expects_list_remove(&blk)
   = 
  request :delete, [:collection_url] do
    instance_eval(&blk) if blk
    let(:request_url) { collection_url }
    let(:deleted_target_ids) { model_collection[0..1] }
    let(:remaining_target_ids) { model_collection - deleted_target_ids }
    let(:request_payload) { { collection_element_name => deleted_target_ids } }
    let(:updated_target_ids) { model.by_parent(model_parent).to_target_ids }

    expects_status(200)
    expects_content_type([:mime_type], [:encoding])

    it 'should delete linked targets' do
      deleted_target_ids.should_not be_empty
      response.should_not be_empty
      deleted_target_ids.each do |deleted_target_id|
        updated_target_ids.should_not include(deleted_target_id)
      end
    end

    it 'should keep remaining targets' do
      deleted_target_ids.should_not be_empty
      response.should_not be_empty
      remaining_target_ids.each do |remaining_target_id|
        updated_target_ids.should include(remaining_target_id)
      end
    end

    with_malformed_data_should_respond_with_400
    with_nil_target_ids_should_respond_with_422
    expects_unauthorized_access_to_respond_with_401
  end
end

#expects_list_replace(&blk) ⇒ Object



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
# File 'lib/intermodal/rspec/requests/linked_resources.rb', line 54

def expects_list_replace(&blk)
   = 
  request :post, [:collection_url] do
    let(:request_url) { collection_url }
    let(:replacement_target_ids) { unlinked_targets.map(&:id) }
    let(:request_payload) { { collection_element_name => replacement_target_ids } }
    let(:updated_target_ids) { model.by_parent(model_parent).to_target_ids }

    instance_eval(&blk) if blk

    expects_status(201)
    expects_content_type([:mime_type], [:encoding])

    with_malformed_data_should_respond_with_400
    with_nil_target_ids_should_respond_with_422
    expects_unauthorized_access_to_respond_with_401

    context 'with empty payload' do
      let(:request_payload) { { collection_element_name => [] } }

      expects_status(201)
      expects_content_type([:mime_type], [:encoding])

      it 'should reset resource linking' do
        response.should_not be_empty
        updated_target_ids.should be_empty
      end
    end

    it "should link #{[:target_resources]} to #{[:parent_resource]}" do
      model_collection.should_not be_empty
      response.should_not be_empty
      replacement_target_ids.each do |replacement_target_id|
        updated_target_ids.should include(replacement_target_id)
      end
    end

    it "should delete original linked #{[:target_resources]}" do
      model_collection.should_not be_empty
      response.should_not be_empty
      model_collection.each do |original_target_id|
        updated_target_ids.should_not include(original_target_id)
      end
    end
  end
end


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
# File 'lib/intermodal/rspec/requests/linked_resources.rb', line 11

def link_resource(parent_resource, options = {}, &blk)
  [:parent_resource] = options[:parent_resource] = parent_resource
  [:target_resources] = options[:to]
  [:linking_resource_model] = options[:with]

  _target_resources = options[:to]

  resource_options = {
    :namespace => options[:namespace]
  }
  resources [[:parent_resource].to_s.pluralize, [:target_resources]], resource_options do
    let(:model) { options[:with] }
    let(:model_parents) { [ send(options[:parent_resource]) ] }
    let(:unlinked_targets) { raise "Override :unlinked_targets with let()" }
    let(:target_resources) { _target_resources }
    let(:collection_element_name) { "#{target_resources.to_s.singularize}_ids" }
    let(:paginated_collection) { collection }
    let(:resource_element_name) { parent_resource }
    let(:presentation_root) { options[:parent_resource] }
    let(:collection_proxy) do
      Intermodal::Proxies::LinkingResources.new presentation_root,
        :to => target_resources,
        :with => collection,
        :parent_id => model_parents.first.id
    end
    let(:presented_collection) { parser.decode(collection_proxy.send("to_#{format}")) }
    instance_eval(&blk)
  end
end

#with_nil_target_ids_should_respond_with_422Object



170
171
172
173
174
175
# File 'lib/intermodal/rspec/requests/linked_resources.rb', line 170

def with_nil_target_ids_should_respond_with_422
  context 'without nil ids ' do
    let(:request_payload) { { collection_element_name => nil } }
    expects_status(422)
  end
end