cirandas.net

ref: master

test/api/categories_test.rb


  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
122
123
124
125
126
127
128
129
130
131
132
133
134
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require_relative 'test_helper'

class CategoriesTest < ActiveSupport::TestCase

  def setup
    create_and_activate_user
  end

  should 'logged user list categories' do
    login_api
    category = fast_create(Category, :environment_id => environment.id)
    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_includes json.map { |c| c["name"] }, category.name
  end

  should 'get category by id to logged user' do
    login_api
    category = fast_create(Category, :environment_id => environment.id)
    get "/api/v1/categories/#{category.id}/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal category.name, json["name"]
  end

  should 'list parent and children when get category by id to logged user' do
    login_api
    parent = fast_create(Category, :environment_id => environment.id)
    child_1 = fast_create(Category, :environment_id => environment.id)
    child_2 = fast_create(Category, :environment_id => environment.id)

    category = fast_create(Category, :environment_id => environment.id)
    category.parent = parent
    category.children << child_1
    category.children << child_2
    category.save

    get "/api/v1/categories/#{category.id}/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal({'id' => parent.id, 'name' => parent.name, 'slug' => parent.slug}, json['parent'])
    assert_equivalent [child_1.id, child_2.id], json['children'].map { |c| c['id'] }
  end

  should 'include parent in categories list if params is true to logged_user' do
    login_api
    parent_1 = fast_create(Category, :environment_id => environment.id) # parent_1 has no parent category
    child_1 = fast_create(Category, :environment_id => environment.id)
    child_2 = fast_create(Category, :environment_id => environment.id)

    parent_2 = fast_create(Category, :environment_id => environment.id)
    parent_2.parent = parent_1
    parent_2.children << child_1
    parent_2.children << child_2
    parent_2.save

    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [nil], json.map { |c| c['parent'] }.uniq

    params[:include_parent] = true
    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [parent_1.parent, parent_2.parent.id, child_1.parent.id, child_2.parent.id],
      json.map { |c| c['parent'] && c['parent']['id'] }
  end

  should 'include children in categories list if params is true to logged user' do
    login_api
    category = fast_create(Category, :environment_id => environment.id)
    child_1 = fast_create(Category, :environment_id => environment.id)
    child_2 = fast_create(Category, :environment_id => environment.id)
    child_3 = fast_create(Category, :environment_id => environment.id)

    category.children << child_1
    category.children << child_2
    category.save

    child_1.children << child_3
    child_1.save

    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [nil], json.map { |c| c['children'] }.uniq

    params[:include_children] = true
    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [category.children.map(&:id).sort, child_1.children.map(&:id).sort, child_2.children.map(&:id).sort, child_3.children.map(&:id).sort],
      json.map{ |c| c['children'].map{ |child| child['id'] }.sort  }
  end

  expose_attributes = %w(id name full_name image display_color)

  expose_attributes.each do |attr|
    should "expose category #{attr} attribute by default to logged user" do
      login_api
      category = fast_create(Category, :environment_id => environment.id)
      get "/api/v1/categories/?#{params.to_query}"
      json = JSON.parse(last_response.body)
     assert json.last.has_key?(attr)
    end
  end

  should 'list categories to anonymous' do
    category = fast_create(Category, :environment_id => environment.id)
    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_includes json.map { |c| c["name"] }, category.name
  end

  should 'get category by id to anonymous' do
    category = fast_create(Category, :environment_id => environment.id)
    get "/api/v1/categories/#{category.id}/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal category.name, json["name"]
  end

  should 'list parent and children when get category by id to anonymous' do
    parent = fast_create(Category, :environment_id => environment.id)
    child_1 = fast_create(Category, :environment_id => environment.id)
    child_2 = fast_create(Category, :environment_id => environment.id)

    category = fast_create(Category, :environment_id => environment.id)
    category.parent = parent
    category.children << child_1
    category.children << child_2
    category.save

    get "/api/v1/categories/#{category.id}/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal({'id' => parent.id, 'name' => parent.name, 'slug' => parent.slug}, json['parent'])
    assert_equivalent [child_1.id, child_2.id], json['children'].map { |c| c['id'] }
  end

  should 'anonymous include parent in categories list if params is true' do
    parent_1 = fast_create(Category, :environment_id => environment.id) # parent_1 has no parent category
    child_1 = fast_create(Category, :environment_id => environment.id)
    child_2 = fast_create(Category, :environment_id => environment.id)

    parent_2 = fast_create(Category, :environment_id => environment.id)
    parent_2.parent = parent_1
    parent_2.children << child_1
    parent_2.children << child_2
    parent_2.save

    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [nil], json.map { |c| c['parent'] }.uniq

    params[:include_parent] = true
    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [parent_1.parent, parent_2.parent.id, child_1.parent.id, child_2.parent.id],
      json.map { |c| c['parent'] && c['parent']['id'] }
  end

  should 'anonymous include children in categories list if params is true' do
    category = fast_create(Category, :environment_id => environment.id)
    child_1 = fast_create(Category, :environment_id => environment.id)
    child_2 = fast_create(Category, :environment_id => environment.id)
    child_3 = fast_create(Category, :environment_id => environment.id)

    category.children << child_1
    category.children << child_2
    category.save

    child_1.children << child_3
    child_1.save

    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [nil], json.map { |c| c['children'] }.uniq

    params[:include_children] = true
    get "/api/v1/categories/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [category.children.map(&:id).sort, child_1.children.map(&:id).sort, child_2.children.map(&:id).sort, child_3.children.map(&:id).sort],
      json.map{ |c| c['children'].map{ |child| child['id'] }.sort  }
  end

  expose_attributes.each do |attr|
    should "expose category #{attr} attribute by default to anonymous" do
      category = fast_create(Category, :environment_id => environment.id)
      get "/api/v1/categories/?#{params.to_query}"
      json = JSON.parse(last_response.body)
     assert json.last.has_key?(attr)
    end
  end

end