solidus_reviews 1.2.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +6 -0
  3. data/.gem_release.yml +5 -0
  4. data/.gitignore +13 -6
  5. data/.rspec +1 -1
  6. data/.rubocop.yml +3 -321
  7. data/.rubocop_todo.yml +197 -0
  8. data/CHANGELOG.md +155 -0
  9. data/Gemfile +14 -17
  10. data/{LICENSE.md → LICENSE} +1 -1
  11. data/README.md +63 -41
  12. data/Rakefile +3 -20
  13. data/app/assets/stylesheets/spree/frontend/solidus_reviews.css +4 -0
  14. data/app/controllers/spree/feedback_reviews_controller.rb +2 -8
  15. data/app/controllers/spree/reviews_controller.rb +29 -2
  16. data/app/decorators/controllers/solidus_reviews/spree/{admin/products_controller_decorator.rb → products_controller_decorator.rb} +0 -4
  17. data/app/decorators/models/solidus_reviews/spree/product_decorator.rb +6 -5
  18. data/app/helpers/spree/reviews_helper.rb +6 -6
  19. data/app/models/spree/feedback_review.rb +2 -2
  20. data/app/models/spree/permission_sets/review_display.rb +11 -0
  21. data/app/models/spree/permission_sets/review_management.rb +11 -0
  22. data/app/models/spree/review.rb +25 -10
  23. data/app/models/spree/reviews_ability.rb +7 -2
  24. data/app/models/spree/reviews_configuration.rb +10 -1
  25. data/app/overrides/add_reviews_tab_to_admin.rb +12 -4
  26. data/app/overrides/add_reviews_to_admin_configuration_sidebar.rb +12 -5
  27. data/app/views/spree/admin/review_settings/edit.html.erb +7 -0
  28. data/app/views/spree/admin/reviews/edit.html.erb +4 -2
  29. data/app/views/spree/admin/reviews/index.html.erb +9 -5
  30. data/app/views/spree/api/reviews/_feedback_review.json.jbuilder +1 -1
  31. data/app/views/spree/api/reviews/_review.json.jbuilder +1 -1
  32. data/app/views/spree/reviews/_form.html.erb +6 -4
  33. data/app/views/spree/reviews/edit.html.erb +3 -0
  34. data/app/views/spree/shared/_review.html.erb +6 -4
  35. data/bin/console +17 -0
  36. data/bin/rails +5 -5
  37. data/bin/rails-engine +13 -0
  38. data/bin/rails-sandbox +16 -0
  39. data/bin/rake +7 -0
  40. data/bin/sandbox +86 -0
  41. data/bin/setup +8 -0
  42. data/config/locales/de-CH.yml +1 -1
  43. data/config/locales/de.yml +1 -1
  44. data/config/locales/en-GB.yml +2 -1
  45. data/config/locales/en.yml +2 -1
  46. data/config/locales/es.yml +2 -1
  47. data/config/locales/fr.yml +1 -1
  48. data/config/locales/it.yml +1 -1
  49. data/config/locales/pl.yml +1 -1
  50. data/config/locales/pt-BR.yml +1 -1
  51. data/config/locales/pt.yml +1 -1
  52. data/config/locales/ro.yml +1 -1
  53. data/config/locales/sv.yml +1 -1
  54. data/config/locales/tr.yml +1 -1
  55. data/config/locales/uk.yml +1 -1
  56. data/config/locales/zh-CN.yml +1 -1
  57. data/config/locales/zh-TW.yml +1 -1
  58. data/config/routes.rb +3 -1
  59. data/db/migrate/20120123141326_recalculate_ratings.rb +1 -2
  60. data/db/migrate/20190613165528_add_verified_purchaser_to_reviews.rb +2 -0
  61. data/lib/controllers/spree/api/feedback_reviews_controller.rb +91 -0
  62. data/lib/controllers/spree/api/reviews_controller.rb +16 -14
  63. data/lib/generators/solidus_reviews/install/install_generator.rb +8 -7
  64. data/lib/solidus_reviews/engine.rb +26 -0
  65. data/lib/solidus_reviews/version.rb +5 -0
  66. data/lib/solidus_reviews.rb +4 -4
  67. data/solidus_reviews.gemspec +31 -34
  68. data/spec/controllers/spree/admin/feedback_reviews_controller_spec.rb +1 -1
  69. data/spec/controllers/spree/admin/review_settings_controller_spec.rb +3 -4
  70. data/spec/controllers/spree/admin/reviews_controller_spec.rb +3 -3
  71. data/spec/controllers/spree/api/feedback_reviews_controller_spec.rb +136 -0
  72. data/spec/controllers/spree/api/reviews_controller_spec.rb +27 -25
  73. data/spec/controllers/spree/feedback_reviews_controller_spec.rb +8 -8
  74. data/spec/controllers/spree/reviews_controller_spec.rb +145 -8
  75. data/spec/features/admin_spec.rb +6 -6
  76. data/spec/features/reviews_spec.rb +33 -32
  77. data/spec/fixtures/thinking-cat.jpg +0 -0
  78. data/spec/models/feedback_review_spec.rb +13 -13
  79. data/spec/models/product_spec.rb +13 -10
  80. data/spec/models/review_spec.rb +64 -41
  81. data/spec/models/reviews_ability_spec.rb +14 -14
  82. data/spec/models/reviews_configuration_spec.rb +21 -10
  83. data/spec/spec_helper.rb +18 -13
  84. data/spec/support/config.rb +7 -0
  85. data/spec/support/factories.rb +3 -0
  86. metadata +52 -213
  87. data/CONTRIBUTING.md +0 -28
  88. data/lib/spree_reviews/engine.rb +0 -22
  89. data/spec/controllers/spree/products_controller_spec.rb +0 -11
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spree
4
+ module Api
5
+ class FeedbackReviewsController < Spree::Api::BaseController
6
+ respond_to :json
7
+
8
+ before_action :load_review, only: [:create, :update, :destroy]
9
+ before_action :load_feedback_review, only: [:update, :destroy]
10
+ before_action :find_review_user
11
+ before_action :sanitize_rating, only: [:create, :update]
12
+ before_action :prevent_multiple_feedback_reviews, only: [:create]
13
+
14
+ def create
15
+ if @review.present?
16
+ @feedback_review = @review.feedback_reviews.new(feedback_review_params)
17
+ @feedback_review.user = @current_api_user
18
+ @feedback_review.locale = I18n.locale.to_s if Spree::Reviews::Config[:track_locale]
19
+ end
20
+
21
+ authorize! :create, @feedback_review
22
+ if @feedback_review.save
23
+ render json: @feedback_review, status: :created
24
+ else
25
+ invalid_resource!(@feedback_review)
26
+ end
27
+ end
28
+
29
+ def update
30
+ authorize! :update, @feedback_review
31
+
32
+ if @feedback_review.update(feedback_review_params)
33
+ render json: @feedback_review, status: :ok
34
+ else
35
+ invalid_resource!(@feedback_review)
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ authorize! :destroy, @feedback_review
41
+
42
+ if @feedback_review.destroy
43
+ render json: @feedback_review, status: :ok
44
+ else
45
+ invalid_resource!(@feedback_review)
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def permitted_feedback_review_attributes
52
+ [:rating, :comment]
53
+ end
54
+
55
+ def feedback_review_params
56
+ params.require(:feedback_review).permit(permitted_feedback_review_attributes)
57
+ end
58
+
59
+ # Finds user based on api_key or by user_id if api_key belongs to an admin.
60
+ def find_review_user
61
+ if params[:user_id] && @current_user_roles.include?('admin')
62
+ @current_api_user = Spree.user_class.find(params[:user_id])
63
+ end
64
+ end
65
+
66
+ # Loads any review that is shared between the user and product
67
+ def load_review
68
+ @review = Spree::Review.find(params[:review_id])
69
+ end
70
+
71
+ # Loads the feedback_review
72
+ def load_feedback_review
73
+ @feedback_review = Spree::FeedbackReview.find(params[:id])
74
+ end
75
+
76
+ # Ensures that a user can't leave multiple feedbacks on a single review
77
+ def prevent_multiple_feedback_reviews
78
+ @feedback_review = @review.feedback_reviews.find_by(user_id: @current_api_user)
79
+ if @feedback_review.present?
80
+ invalid_resource!(@feedback_review)
81
+ end
82
+ end
83
+
84
+ # Converts rating strings like "5 units" to "5"
85
+ # Operates on params
86
+ def sanitize_rating
87
+ params[:rating].to_s.sub!(/\s*[^0-9]*\z/, '') unless params[:feedback_review] && params[:feedback_review][:rating].blank?
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Spree
2
4
  module Api
3
5
  class ReviewsController < Spree::Api::BaseController
@@ -9,11 +11,11 @@ module Spree
9
11
  before_action :prevent_multiple_reviews, only: [:create]
10
12
 
11
13
  def index
12
- if @product
13
- @reviews = Spree::Review.default_approval_filter.where(product: @product)
14
- else
15
- @reviews = Spree::Review.where(user: @current_api_user)
16
- end
14
+ @reviews = if @product
15
+ Spree::Review.default_approval_filter.where(product: @product)
16
+ else
17
+ Spree::Review.where(user: @current_api_user)
18
+ end
17
19
 
18
20
  respond_with(@reviews)
19
21
  end
@@ -34,7 +36,7 @@ module Spree
34
36
 
35
37
  authorize! :create, @review
36
38
  if @review.save
37
- render json: @review, include: [:images, :feedback_reviews], status: 201
39
+ render json: @review, include: [:images, :feedback_reviews], status: :created
38
40
  else
39
41
  invalid_resource!(@review)
40
42
  end
@@ -46,7 +48,7 @@ module Spree
46
48
  attributes = review_params.merge(ip_address: request.remote_ip, approved: false)
47
49
 
48
50
  if @review.update(attributes)
49
- render json: @review, include: [:images, :feedback_reviews], status: 200
51
+ render json: @review, include: [:images, :feedback_reviews], status: :ok
50
52
  else
51
53
  invalid_resource!(@review)
52
54
  end
@@ -56,7 +58,7 @@ module Spree
56
58
  authorize! :destroy, @review
57
59
 
58
60
  if @review.destroy
59
- render json: @review, status: 200
61
+ render json: @review, status: :ok
60
62
  else
61
63
  invalid_resource!(@review)
62
64
  end
@@ -74,11 +76,11 @@ module Spree
74
76
 
75
77
  # Loads product from product id.
76
78
  def load_product
77
- if params[:product_id]
78
- @product = Spree::Product.friendly.find(params[:product_id])
79
- else
80
- @product = @review&.product
81
- end
79
+ @product = if params[:product_id]
80
+ Spree::Product.friendly.find(params[:product_id])
81
+ else
82
+ @review&.product
83
+ end
82
84
  end
83
85
 
84
86
  # Finds user based on api_key or by user_id if api_key belongs to an admin.
@@ -104,7 +106,7 @@ module Spree
104
106
  # Converts rating strings like "5 units" to "5"
105
107
  # Operates on params
106
108
  def sanitize_rating
107
- params[:rating].sub!(/\s*[^0-9]*\z/, '') unless params[:rating].blank?
109
+ params[:rating].sub!(/\s*[^0-9]*\z/, '') if params[:rating].present?
108
110
  end
109
111
  end
110
112
  end
@@ -6,24 +6,25 @@ module SolidusReviews
6
6
  class_option :auto_run_migrations, type: :boolean, default: false
7
7
 
8
8
  def add_javascripts
9
- append_file "vendor/assets/javascripts/spree/frontend/all.js", "//= require spree/frontend/solidus_reviews\n"
10
- append_file "vendor/assets/javascripts/spree/backend/all.js", "//= require spree/backend/solidus_reviews\n"
9
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_reviews\n"
10
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/solidus_reviews\n"
11
11
  end
12
12
 
13
13
  def add_stylesheets
14
- inject_into_file "vendor/assets/stylesheets/spree/frontend/all.css", " *= require spree/frontend/solidus_reviews\n", before: /\*\//, verbose: true
14
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_reviews\n", before: %r{\*/}, verbose: true # rubocop:disable Layout/LineLength
15
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_reviews\n", before: %r{\*/}, verbose: true # rubocop:disable Layout/LineLength
15
16
  end
16
17
 
17
18
  def add_migrations
18
- run 'bundle exec rake railties:install:migrations FROM=solidus_reviews'
19
+ run 'bin/rails railties:install:migrations FROM=solidus_reviews'
19
20
  end
20
21
 
21
22
  def run_migrations
22
- run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
23
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Layout/LineLength
23
24
  if run_migrations
24
- run 'bundle exec rake db:migrate'
25
+ run 'bin/rails db:migrate'
25
26
  else
26
- puts 'Skipping rake db:migrate, don\'t forget to run it!'
27
+ puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
27
28
  end
28
29
  end
29
30
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spree/core'
4
+
5
+ module SolidusReviews
6
+ class Engine < Rails::Engine
7
+ include SolidusSupport::EngineExtensions
8
+
9
+ isolate_namespace Spree
10
+
11
+ engine_name 'solidus_reviews'
12
+
13
+ # use rspec for tests
14
+ config.generators do |g|
15
+ g.test_framework :rspec
16
+ end
17
+
18
+ config.to_prepare do
19
+ ::Spree::Ability.register_ability(::Spree::ReviewsAbility)
20
+ end
21
+
22
+ if SolidusSupport.api_available?
23
+ paths["app/controllers"] << "lib/controllers"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusReviews
4
+ VERSION = '1.5.0'
5
+ end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'solidus'
3
+ require 'solidus_core'
4
4
  require 'solidus_support'
5
- require 'sass/rails'
6
5
  require 'deface'
7
- require 'spree_reviews/engine'
8
- require 'coffee_script'
6
+
7
+ require 'solidus_reviews/version'
8
+ require 'solidus_reviews/engine'
@@ -1,37 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- Gem::Specification.new do |s|
4
- s.platform = Gem::Platform::RUBY
5
- s.name = 'solidus_reviews'
6
- s.version = '1.2.0'
7
- s.summary = 'Basic review and ratings facility for Solidus'
8
- s.authors = ['Solidus Contrib']
9
- s.description = s.summary
10
- s.required_ruby_version = '>= 1.9.3'
11
-
12
- s.homepage = 'https://github.com/solidusio-contrib/solidus_reviews/'
13
- s.license = 'BSD-3'
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- spec/*`.split("\n")
17
- s.require_path = 'lib'
18
- s.requirements << 'none'
19
-
20
- s.add_dependency 'deface', '~> 1'
21
- s.add_dependency 'solidus', ['>= 1.4', '< 3']
22
- s.add_dependency 'solidus_support', '~> 0.1'
23
-
24
- s.add_development_dependency 'capybara'
25
- s.add_development_dependency 'capybara-screenshot'
26
- s.add_development_dependency 'coffee-rails'
27
- s.add_development_dependency 'database_cleaner'
28
- s.add_development_dependency 'ffaker'
29
- s.add_development_dependency 'poltergeist'
30
- s.add_development_dependency 'pry'
31
- s.add_development_dependency 'rspec-rails', '~> 4.0.0.beta2'
32
- s.add_development_dependency 'rubocop', '~> 0.53.0'
33
- s.add_development_dependency 'sass-rails'
34
- s.add_development_dependency 'selenium-webdriver'
35
- s.add_development_dependency 'simplecov'
36
- s.add_development_dependency 'sqlite3'
3
+ require_relative 'lib/solidus_reviews/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'solidus_reviews'
7
+ spec.version = SolidusReviews::VERSION
8
+ spec.authors = ['Solidus Contrib']
9
+
10
+ spec.summary = 'Review and rating functionality for your Solidus store.'
11
+ spec.description = 'Review and rating functionality for your Solidus store.'
12
+ spec.homepage = 'https://github.com/solidusio-contrib/solidus_reviews'
13
+ spec.license = 'BSD-3-Clause'
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/solidusio-contrib/solidus_reviews'
17
+
18
+ spec.required_ruby_version = Gem::Requirement.new('> 2.5')
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") }
23
+
24
+ spec.files = files.grep_v(%r{^(test|spec|features)/})
25
+ spec.test_files = files.grep(%r{^(test|spec|features)/})
26
+ spec.bindir = "exe"
27
+ spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 4']
31
+ spec.add_dependency 'solidus_support', '~> 0.5'
32
+
33
+ spec.add_development_dependency 'solidus_dev_support'
37
34
  end
@@ -10,7 +10,7 @@ describe Spree::Admin::FeedbackReviewsController do
10
10
  allow(controller).to receive(:spree_current_user).and_return(user)
11
11
  end
12
12
 
13
- context '#index' do
13
+ describe '#index' do
14
14
  let!(:review) { create(:review) }
15
15
  let!(:other_review) { create(:review) }
16
16
 
@@ -10,7 +10,7 @@ describe Spree::Admin::ReviewSettingsController do
10
10
  allow(controller).to receive(:spree_current_user).and_return(user)
11
11
  end
12
12
 
13
- context '#update' do
13
+ describe '#update' do
14
14
  it 'redirects to edit-review-settings page' do
15
15
  put :update, params: { preferences: { preview_size: 4 } }
16
16
  expect(response).to redirect_to spree.edit_admin_review_settings_path
@@ -22,7 +22,6 @@ describe Spree::Admin::ReviewSettingsController do
22
22
  feedback_rating: false,
23
23
  require_login: true,
24
24
  track_locale: true' do
25
-
26
25
  it 'sets preferred_preview_size to 4' do
27
26
  put :update, params: { preferences: { preview_size: 4 } }
28
27
  expect(Spree::Reviews::Config.preferred_preview_size).to eq 4
@@ -50,8 +49,8 @@ describe Spree::Admin::ReviewSettingsController do
50
49
  end
51
50
  end
52
51
 
53
- context '#edit' do
54
- it 'should render the edit template' do
52
+ describe '#edit' do
53
+ it 'renders the edit template' do
55
54
  get :edit
56
55
  expect(response).to render_template(:edit)
57
56
  end
@@ -13,7 +13,7 @@ describe Spree::Admin::ReviewsController do
13
13
  allow(controller).to receive(:spree_current_user).and_return(user)
14
14
  end
15
15
 
16
- context '#index' do
16
+ describe '#index' do
17
17
  it 'list reviews' do
18
18
  reviews = [
19
19
  create(:review, product: product),
@@ -24,7 +24,7 @@ describe Spree::Admin::ReviewsController do
24
24
  end
25
25
  end
26
26
 
27
- context '#approve' do
27
+ describe '#approve' do
28
28
  it 'show notice message when approved' do
29
29
  review.update_attribute(:approved, true)
30
30
  get :approve, params: { id: review.id }
@@ -39,7 +39,7 @@ describe Spree::Admin::ReviewsController do
39
39
  end
40
40
  end
41
41
 
42
- context '#edit' do
42
+ describe '#edit' do
43
43
  specify do
44
44
  get :edit, params: { id: review.id }
45
45
  expect(response.status).to eq(200)
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Spree::Api::FeedbackReviewsController, type: :controller do
6
+ render_views
7
+
8
+ let!(:user) { create(:user) }
9
+ let!(:review) { create(:review) }
10
+ let!(:feedback_review) { create(:feedback_review, review: review) }
11
+
12
+ before do
13
+ user.generate_spree_api_key!
14
+ end
15
+
16
+ describe '#create' do
17
+ subject do
18
+ params = { review_id: review.id, token: user.spree_api_key, format: 'json' }.merge(feedback_review_params)
19
+ post :create, params: params
20
+ JSON.parse(response.body)
21
+ end
22
+
23
+ let(:feedback_review_params) do
24
+ {
25
+ "feedback_review": {
26
+ "rating": "5",
27
+ "comment": "I agree with what you said"
28
+ }
29
+ }
30
+ end
31
+
32
+ context 'when user has already left feedback on a reviewed this product' do
33
+ before do
34
+ feedback_review.update(user_id: user.id)
35
+ end
36
+
37
+ it 'returns with a fail' do
38
+ expect(subject["error"]).not_to be_empty
39
+ expect(subject["error"]).to match(/invalid resource/i)
40
+ end
41
+ end
42
+
43
+ context 'when it is a users first feedback for a review' do
44
+ it 'returns success with feedback' do
45
+ expect(subject).not_to be_empty
46
+ expect(subject["review_id"]).to eq(review.id)
47
+ expect(subject["rating"]).to eq(5)
48
+ expect(subject["comment"]).to eq("I agree with what you said")
49
+ end
50
+
51
+ it 'updates the review' do
52
+ expect(review).to receive(:touch)
53
+ feedback = create(:feedback_review, review: review)
54
+ feedback.save!
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#update' do
60
+ subject do
61
+ put :update, params: params
62
+ JSON.parse(response.body)
63
+ end
64
+
65
+ before { feedback_review.update(user_id: user.id) }
66
+
67
+ let(:params) { { review_id: review.id, id: feedback_review.id, token: user.spree_api_key, format: 'json' }.merge(feedback_review_params) }
68
+
69
+ let(:feedback_review_params) do
70
+ {
71
+ "feedback_review": {
72
+ "rating": "1",
73
+ "comment": "Actually I don't agree"
74
+ }
75
+ }
76
+ end
77
+
78
+ context 'when a user updates their own feedback for a review' do
79
+ it 'successfully updates their feedback' do
80
+ original = feedback_review
81
+ expect(subject["id"]).to eq(original.id)
82
+ expect(subject["user_id"]).to eq(original.user_id)
83
+ expect(subject["review_id"]).to eq(original.review_id)
84
+ expect(subject["rating"]).to eq(1)
85
+ expect(subject["comment"]).to eq("Actually I don't agree")
86
+ end
87
+ end
88
+
89
+ context 'when a user updates another users review' do
90
+ let(:other_user) { create(:user) }
91
+ let(:params) { { review_id: review.id, id: feedback_review.id, token: other_user.spree_api_key, format: 'json' }.merge(feedback_review_params) }
92
+
93
+ before do
94
+ other_user.generate_spree_api_key!
95
+ end
96
+
97
+ it 'returns an error' do
98
+ expect(subject["error"]).not_to be_empty
99
+ expect(subject["error"]).to match(/not authorized/i)
100
+ end
101
+ end
102
+ end
103
+
104
+ describe '#destroy' do
105
+ subject do
106
+ delete :destroy, params: params
107
+ JSON.parse(response.body)
108
+ end
109
+
110
+ before { feedback_review.update(user_id: user.id) }
111
+
112
+ let(:params) { { review_id: review.id, id: feedback_review.id, token: user.spree_api_key, format: 'json' } }
113
+
114
+ context "when a user destroys their own feedback" do
115
+ it 'returns the deleted feedback' do
116
+ expect(subject["id"]).to eq(feedback_review.id)
117
+ expect(subject["review_id"]).to eq(review.id)
118
+ expect(Spree::FeedbackReview.find_by(id: feedback_review.id)).to be_falsey
119
+ end
120
+ end
121
+
122
+ context "when a user destroys another users feedback" do
123
+ let(:other_user) { create(:user) }
124
+ let(:params) { { review_id: review.id, id: feedback_review.id, token: other_user.spree_api_key, format: 'json' } }
125
+
126
+ before do
127
+ other_user.generate_spree_api_key!
128
+ end
129
+
130
+ it 'returns an error' do
131
+ expect(subject["error"]).not_to be_empty
132
+ expect(subject["error"]).to match(/not authorized/i)
133
+ end
134
+ end
135
+ end
136
+ end
@@ -24,7 +24,7 @@ describe Spree::Api::ReviewsController, type: :controller do
24
24
  end
25
25
 
26
26
  context 'there are no reviews for a product' do
27
- it 'should return an empty array' do
27
+ it 'returns an empty array' do
28
28
  expect(Spree::Review.count).to be >= 0
29
29
  expect(subject["reviews"]).to be_empty
30
30
  end
@@ -53,7 +53,7 @@ describe Spree::Api::ReviewsController, type: :controller do
53
53
  end
54
54
 
55
55
  context 'there are no reviews for the user' do
56
- it 'should return an empty array' do
56
+ it 'returns an empty array' do
57
57
  expect(Spree::Review.count).to be >= 0
58
58
  expect(subject["reviews"]).to be_empty
59
59
  end
@@ -81,7 +81,7 @@ describe Spree::Api::ReviewsController, type: :controller do
81
81
  context 'when it is the users review' do
82
82
  before { review.update(user_id: user.id) }
83
83
 
84
- it 'should return the review' do
84
+ it 'returns the review' do
85
85
  expect(subject).not_to be_empty
86
86
  expect(subject["product_id"]).to eq(product.id)
87
87
  expect(subject["name"]).to eq(review[:name])
@@ -94,7 +94,7 @@ describe Spree::Api::ReviewsController, type: :controller do
94
94
  end
95
95
 
96
96
  context 'when it is not the users review' do
97
- it 'should return with not authorized' do
97
+ it 'returns with not authorized' do
98
98
  expect(subject["error"]).not_to be_empty
99
99
  expect(subject["error"]).to match(/not authorized/i)
100
100
  end
@@ -102,7 +102,7 @@ describe Spree::Api::ReviewsController, type: :controller do
102
102
  context 'and it the review is approved' do
103
103
  before { review.update(approved: true) }
104
104
 
105
- it 'should return the review' do
105
+ it 'returns the review' do
106
106
  expect(subject).not_to be_empty
107
107
  expect(subject["product_id"]).to eq(product.id)
108
108
  expect(subject["name"]).to eq(review[:name])
@@ -116,6 +116,12 @@ describe Spree::Api::ReviewsController, type: :controller do
116
116
  end
117
117
 
118
118
  describe '#create' do
119
+ subject do
120
+ params = { product_id: product.id, token: user.spree_api_key, format: 'json' }.merge(review_params)
121
+ post :create, params: params
122
+ JSON.parse(response.body)
123
+ end
124
+
119
125
  let(:review_params) do
120
126
  {
121
127
  "user_id": user.id,
@@ -126,25 +132,19 @@ describe Spree::Api::ReviewsController, type: :controller do
126
132
  }
127
133
  end
128
134
 
129
- subject do
130
- params = { product_id: product.id, token: user.spree_api_key, format: 'json' }.merge(review_params)
131
- post :create, params: params
132
- JSON.parse(response.body)
133
- end
134
-
135
135
  context 'when user has already reviewed this product' do
136
136
  before do
137
137
  review.update(user_id: user.id)
138
138
  end
139
139
 
140
- it 'should return with a fail' do
140
+ it 'returns with a fail' do
141
141
  expect(subject["error"]).not_to be_empty
142
142
  expect(subject["error"]).to match(/invalid resource/i)
143
143
  end
144
144
  end
145
145
 
146
146
  context 'when it is a users first review for the product' do
147
- it 'should return success with review' do
147
+ it 'returns success with review' do
148
148
  expect(subject).not_to be_empty
149
149
  expect(subject["product_id"]).to eq(product.id)
150
150
  expect(subject["name"]).to eq(review_params[:name])
@@ -157,7 +157,13 @@ describe Spree::Api::ReviewsController, type: :controller do
157
157
  end
158
158
 
159
159
  describe '#update' do
160
+ subject do
161
+ put :update, params: params
162
+ JSON.parse(response.body)
163
+ end
164
+
160
165
  before { review.update(approved: true, user_id: user.id) }
166
+
161
167
  let(:params) { { product_id: product.id, id: review.id, token: user.spree_api_key, format: 'json' }.merge(review_params) }
162
168
 
163
169
  let(:review_params) do
@@ -169,13 +175,8 @@ describe Spree::Api::ReviewsController, type: :controller do
169
175
  }
170
176
  end
171
177
 
172
- subject do
173
- put :update, params: params
174
- JSON.parse(response.body)
175
- end
176
-
177
178
  context 'when a user updates their own review' do
178
- it 'should successfully update the review and set approved back to false' do
179
+ it 'successfullies update the review and set approved back to false' do
179
180
  original = review
180
181
  expect(original.approved?).to be true
181
182
  expect(subject["id"]).to eq(original.id)
@@ -195,7 +196,7 @@ describe Spree::Api::ReviewsController, type: :controller do
195
196
  other_user.generate_spree_api_key!
196
197
  end
197
198
 
198
- it 'should return an error' do
199
+ it 'returns an error' do
199
200
  expect(subject["error"]).not_to be_empty
200
201
  expect(subject["error"]).to match(/not authorized/i)
201
202
  end
@@ -203,16 +204,17 @@ describe Spree::Api::ReviewsController, type: :controller do
203
204
  end
204
205
 
205
206
  describe '#destroy' do
206
- before { review.update(approved: true, user_id: user.id) }
207
- let(:params) { { product_id: product.id, id: review.id, token: user.spree_api_key, format: 'json' } }
208
-
209
207
  subject do
210
208
  delete :destroy, params: params
211
209
  JSON.parse(response.body)
212
210
  end
213
211
 
212
+ before { review.update(approved: true, user_id: user.id) }
213
+
214
+ let(:params) { { product_id: product.id, id: review.id, token: user.spree_api_key, format: 'json' } }
215
+
214
216
  context "when a user destroys their own review" do
215
- it 'should return the deleted review' do
217
+ it 'returns the deleted review' do
216
218
  expect(subject["id"]).to eq(review.id)
217
219
  expect(subject["product_id"]).to eq(product.id)
218
220
  expect(Spree::Review.find_by(id: review.id)).to be_falsey
@@ -227,7 +229,7 @@ describe Spree::Api::ReviewsController, type: :controller do
227
229
  other_user.generate_spree_api_key!
228
230
  end
229
231
 
230
- it 'should return an error' do
232
+ it 'returns an error' do
231
233
  expect(subject["error"]).not_to be_empty
232
234
  expect(subject["error"]).to match(/not authorized/i)
233
235
  end