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.
- checksums.yaml +4 -4
- data/.circleci/config.yml +6 -0
- data/.gem_release.yml +5 -0
- data/.gitignore +13 -6
- data/.rspec +1 -1
- data/.rubocop.yml +3 -321
- data/.rubocop_todo.yml +197 -0
- data/CHANGELOG.md +155 -0
- data/Gemfile +14 -17
- data/{LICENSE.md → LICENSE} +1 -1
- data/README.md +63 -41
- data/Rakefile +3 -20
- data/app/assets/stylesheets/spree/frontend/solidus_reviews.css +4 -0
- data/app/controllers/spree/feedback_reviews_controller.rb +2 -8
- data/app/controllers/spree/reviews_controller.rb +29 -2
- data/app/decorators/controllers/solidus_reviews/spree/{admin/products_controller_decorator.rb → products_controller_decorator.rb} +0 -4
- data/app/decorators/models/solidus_reviews/spree/product_decorator.rb +6 -5
- data/app/helpers/spree/reviews_helper.rb +6 -6
- data/app/models/spree/feedback_review.rb +2 -2
- data/app/models/spree/permission_sets/review_display.rb +11 -0
- data/app/models/spree/permission_sets/review_management.rb +11 -0
- data/app/models/spree/review.rb +25 -10
- data/app/models/spree/reviews_ability.rb +7 -2
- data/app/models/spree/reviews_configuration.rb +10 -1
- data/app/overrides/add_reviews_tab_to_admin.rb +12 -4
- data/app/overrides/add_reviews_to_admin_configuration_sidebar.rb +12 -5
- data/app/views/spree/admin/review_settings/edit.html.erb +7 -0
- data/app/views/spree/admin/reviews/edit.html.erb +4 -2
- data/app/views/spree/admin/reviews/index.html.erb +9 -5
- data/app/views/spree/api/reviews/_feedback_review.json.jbuilder +1 -1
- data/app/views/spree/api/reviews/_review.json.jbuilder +1 -1
- data/app/views/spree/reviews/_form.html.erb +6 -4
- data/app/views/spree/reviews/edit.html.erb +3 -0
- data/app/views/spree/shared/_review.html.erb +6 -4
- data/bin/console +17 -0
- data/bin/rails +5 -5
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +16 -0
- data/bin/rake +7 -0
- data/bin/sandbox +86 -0
- data/bin/setup +8 -0
- data/config/locales/de-CH.yml +1 -1
- data/config/locales/de.yml +1 -1
- data/config/locales/en-GB.yml +2 -1
- data/config/locales/en.yml +2 -1
- data/config/locales/es.yml +2 -1
- data/config/locales/fr.yml +1 -1
- data/config/locales/it.yml +1 -1
- data/config/locales/pl.yml +1 -1
- data/config/locales/pt-BR.yml +1 -1
- data/config/locales/pt.yml +1 -1
- data/config/locales/ro.yml +1 -1
- data/config/locales/sv.yml +1 -1
- data/config/locales/tr.yml +1 -1
- data/config/locales/uk.yml +1 -1
- data/config/locales/zh-CN.yml +1 -1
- data/config/locales/zh-TW.yml +1 -1
- data/config/routes.rb +3 -1
- data/db/migrate/20120123141326_recalculate_ratings.rb +1 -2
- data/db/migrate/20190613165528_add_verified_purchaser_to_reviews.rb +2 -0
- data/lib/controllers/spree/api/feedback_reviews_controller.rb +91 -0
- data/lib/controllers/spree/api/reviews_controller.rb +16 -14
- data/lib/generators/solidus_reviews/install/install_generator.rb +8 -7
- data/lib/solidus_reviews/engine.rb +26 -0
- data/lib/solidus_reviews/version.rb +5 -0
- data/lib/solidus_reviews.rb +4 -4
- data/solidus_reviews.gemspec +31 -34
- data/spec/controllers/spree/admin/feedback_reviews_controller_spec.rb +1 -1
- data/spec/controllers/spree/admin/review_settings_controller_spec.rb +3 -4
- data/spec/controllers/spree/admin/reviews_controller_spec.rb +3 -3
- data/spec/controllers/spree/api/feedback_reviews_controller_spec.rb +136 -0
- data/spec/controllers/spree/api/reviews_controller_spec.rb +27 -25
- data/spec/controllers/spree/feedback_reviews_controller_spec.rb +8 -8
- data/spec/controllers/spree/reviews_controller_spec.rb +145 -8
- data/spec/features/admin_spec.rb +6 -6
- data/spec/features/reviews_spec.rb +33 -32
- data/spec/fixtures/thinking-cat.jpg +0 -0
- data/spec/models/feedback_review_spec.rb +13 -13
- data/spec/models/product_spec.rb +13 -10
- data/spec/models/review_spec.rb +64 -41
- data/spec/models/reviews_ability_spec.rb +14 -14
- data/spec/models/reviews_configuration_spec.rb +21 -10
- data/spec/spec_helper.rb +18 -13
- data/spec/support/config.rb +7 -0
- data/spec/support/factories.rb +3 -0
- metadata +52 -213
- data/CONTRIBUTING.md +0 -28
- data/lib/spree_reviews/engine.rb +0 -22
- 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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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:
|
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:
|
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:
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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/, '')
|
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
|
10
|
-
append_file
|
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
|
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 '
|
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 '
|
25
|
+
run 'bin/rails db:migrate'
|
25
26
|
else
|
26
|
-
puts 'Skipping
|
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
|
data/lib/solidus_reviews.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'solidus_core'
|
4
4
|
require 'solidus_support'
|
5
|
-
require 'sass/rails'
|
6
5
|
require 'deface'
|
7
|
-
|
8
|
-
require '
|
6
|
+
|
7
|
+
require 'solidus_reviews/version'
|
8
|
+
require 'solidus_reviews/engine'
|
data/solidus_reviews.gemspec
CHANGED
@@ -1,37 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
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
|
-
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
|
-
|
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
|
-
|
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
|
-
|
54
|
-
it '
|
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
|
-
|
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
|
-
|
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
|
-
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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 '
|
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
|