Skip to content

Add more spec test #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions spec/requests/admin/object_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# frozen_string_literal: true

require_relative "../../plugin_helper.rb"

describe DiscourseRatings::ObjectController do
let!(:rating_type) { "none" }
let!(:other_rating_type) { "discipline" }
let!(:other_rating_name) { "Discipline" }
let!(:rating_category) { Fabricate(:category) }
let!(:create_rating_type) do
{ type: "category", name: rating_category.rating_key, types: [rating_type] }
end
let!(:update_rating_type) do
{
type: "category",
name: rating_category.rating_key,
types: [rating_type, other_rating_type],
}
end
let!(:delete_rating_type) do
{ type: "category", name: rating_category.rating_key }
end
let!(:create_rating_type_error) do
{
type: "cate",
name: rating_category.rating_key,
types: [other_rating_type],
}
end
let!(:rating_tag) { Fabricate(:tag) }
# it "is a subclass of AdminController" do
# expect(DiscourseRatings::ObjectController < ::Admin::AdminController)
# end
context "authenticated" do
let(:admin) { Fabricate(:admin) }

before { sign_in(admin) }
describe "#create" do
it "errors when type is incorrect" do
post "/ratings/object.json", params: create_rating_type_error
expect(response.status).to eq(400)
expect(response.parsed_body["error_type"]).to eq("invalid_parameters")
expect(rating_category.rating_types).not_to include(other_rating_type)
end
it "errors when category already exists" do
DiscourseRatings::Object.create(
"category",
rating_category.rating_key,
[rating_type]
)
post "/ratings/object.json", params: create_rating_type
expect(response.status).to eq(400)
expect(response.parsed_body["error_type"]).to eq("invalid_parameters")
end
it "creates a new category rating" do
post "/ratings/object.json", params: create_rating_type
expect(response.status).to eq(200)
expect(response.parsed_body["success"]).to eq("OK")
expect(
DiscourseRatings::Object.exists?(
"category",
rating_category.rating_key
)
).to eq(true)
expect(rating_category.rating_types).to include(rating_type)
end
end

describe "#show" do
it "displays a category rating" do
DiscourseRatings::Object.create(
"category",
rating_category.rating_key,
[rating_type]
)
get "/ratings/object/category.json"
expect(response.status).to eq(200)
expect(response.parsed_body.first["name"]).to eq(
rating_category.rating_key
)
expect(response.parsed_body.first["types"]).to eq([rating_type])
end
end

describe "#update" do
it "updates existing category rating" do
DiscourseRatings::Object.create(
"category",
rating_category.rating_key,
[rating_type]
)
DiscourseRatings::RatingType.create(
other_rating_type,
other_rating_name
)
put "/ratings/object/category.json", params: update_rating_type
expect(response.status).to eq(200)
expect(rating_category.rating_types).to include(rating_type)
expect(rating_category.rating_types).to include(other_rating_type)
end
end

describe "#destroy" do
it "destroys a category rating" do
DiscourseRatings::Object.create(
"category",
rating_category.rating_key,
[rating_type]
)
delete "/ratings/object/category.json", params: delete_rating_type
expect(response.status).to eq(200)
expect(
DiscourseRatings::Object.exists?(
"category",
rating_category.rating_key
)
).to eq(false)
end
end
end
end
93 changes: 93 additions & 0 deletions spec/requests/admin/rating_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# frozen_string_literal: true

require_relative "../../plugin_helper.rb"

describe DiscourseRatings::RatingController do
it "is a subclass of AdminController" do
expect(DiscourseRatings::RatingController < ::Admin::AdminController)
end

let!(:rating_type_params) { { type: "discipline", name: "Discipline" } }
let!(:category) { Fabricate(:category) }
let!(:migrate_params) do
{ category_id: category.id, type: "discipline", new_type: "none" }
end
let!(:migrate_params_non_existent) do
{ category_id: category.id, type: "none", new_type: "discipline" }
end
let(:none_rating_hash) do
JSON.parse('[{"type":"none","value":"4", "pavilion": "yes"}]')
end
let!(:destroy_params) { { category_id: category.id } }

context "not logged in" do
describe "#destroy" do
it "blocks non-admin" do
DiscourseRatings::RatingType.create(
rating_type_params[:type],
rating_type_params[:name]
)

delete "/ratings/rating/#{rating_type_params[:type]}.json",
params: destroy_params
expect(response.status).to eq(404)
expect(response.parsed_body["error_type"]).to eq("not_found")
end
end
end

context "authenticated" do
let(:admin) { Fabricate(:admin) }

before { sign_in(admin) }
describe "#migrate" do
it "migrates the rating to other category" do
DiscourseRatings::RatingType.create(
rating_type_params[:type],
rating_type_params[:name]
)

post "/ratings/rating/migrate.json", params: migrate_params
#testing the job if is working or not is not neccesary
expect(response.status).to eq(200)
expect(response.parsed_body["success"]).to eq("OK")

expect(Jobs::MigrateRatings.jobs.size).to eq(1)

job_data = Jobs::MigrateRatings.jobs.first["args"].first
expect(job_data["category_id"].to_i).to eq(category.id)
expect(job_data["type"]).to eq("discipline")
expect(job_data["new_type"]).to eq("none")
end

it "errors when second parameter is invalid" do
post "/ratings/rating/migrate.json", params: migrate_params_non_existent
#testing the job if is working or not is not neccesary
expect(response.status).to eq(400)
expect(response.parsed_body["error_type"]).to eq("invalid_parameters")

expect(Jobs::MigrateRatings.jobs.size).to eq(0)
end

it "errors when first parameters is invalid" do
post "/ratings/rating/migrate.json", params: migrate_params
expect(response.status).to eq(400)
expect(response.parsed_body["error_type"]).to eq("invalid_parameters")
end
end

describe "#destroy" do
it "destroys the rating type" do
DiscourseRatings::RatingType.create(
rating_type_params[:type],
rating_type_params[:name]
)

delete "/ratings/rating/#{rating_type_params[:type]}.json",
params: destroy_params
expect(response.status).to eq(200)
expect(response.parsed_body["success"]).to eq("OK")
end
end
end
end
77 changes: 58 additions & 19 deletions spec/requests/admin/rating_type_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,69 @@
# frozen_string_literal: true

require_relative '../../plugin_helper.rb'
require_relative "../../plugin_helper.rb"

describe DiscourseRatings::RatingTypeController do
let!(:create_params) do
{ type: 'comfort', name: 'Comfort' }
let!(:create_params) { { type: "comfort", name: "Comfort" } }
let!(:update_params) { { type: "comfort", name: "Comfort Zone" } }
let!(:delete_params) { { type: "comfort" } }
fab!(:admin) { sign_in(Fabricate(:admin)) }

describe "#index" do
it "displays the rating type correctly" do
DiscourseRatings::RatingType.create(
create_params[:type],
create_params[:name]
)
get "/ratings/rating-type.json"
expect(response.status).to eq(200)
end
end
let!(:update_params) do
{ type: 'comfort', name: 'Comfort Zone' }
describe "#create" do
it "creates the rating type correctly" do
post "/ratings/rating-type.json", params: create_params
expect(response.status).to eq(200)
expect(DiscourseRatings::RatingType.all.map { |t| t.type }).to include(
create_params[:type]
)
end
fab!(:admin) { sign_in(Fabricate(:admin)) }
end

describe "#create" do
it "creates the rating type correctly" do
post "/ratings/rating-type.json", params: create_params
expect(response.status).to eq(200)
expect(DiscourseRatings::RatingType.all.map { |t| t.type }).to include(create_params[:type])
end
describe "#update" do
it "updates the rating type correctly" do
DiscourseRatings::RatingType.create(
create_params[:type],
create_params[:name]
)
put "/ratings/rating-type/" + update_params[:type] + ".json",
params: update_params
expect(response.status).to eq(200)
expect(DiscourseRatings::RatingType.get_name(create_params[:type])).to eq(
update_params[:name]
)
end
end

describe "#update" do
it "updates the rating type correctly" do
DiscourseRatings::RatingType.create(create_params[:type], create_params[:name])
put "/ratings/rating-type/" + update_params[:type] + ".json", params: update_params
expect(response.status).to eq(200)
expect(DiscourseRatings::RatingType.get_name(create_params[:type])).to eq(update_params[:name])
end
describe "#destroy" do
it "destroys the rating type" do
DiscourseRatings::RatingType.create(
create_params[:type],
create_params[:name]
)
delete "/ratings/rating-type/" + create_params[:type] + ".json",
params: delete_params
expect(response.status).to eq(200)
expect(response.parsed_body["success"]).to eq("OK")
expect(Jobs::DestroyRatingType.jobs.size).to eq(1)
job_data = Jobs::DestroyRatingType.jobs.first["args"].first
expect(job_data["type"]).to eq("comfort")
expect(job_data["current_site_id"]).to eq("default")
end
# it "gives 400 error for invalid data" do
# delete "/ratings/rating-type/" + create_params[:type] + ".json",
# params: delete_params
# expect(response.status).to eq(400)
# expect(response.parsed_body["error_type"]).to eq("invalid_parameters")
# expect(Jobs::DestroyRatingType.jobs.size).to eq(0)
# end
end
end
41 changes: 25 additions & 16 deletions spec/requests/posts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
# frozen_string_literal: true

require_relative '../plugin_helper.rb'
require_relative "../plugin_helper.rb"

describe PostsController do
let(:rating_hash) { JSON.parse('[{"type":"pointers","value":"4", "pavilion": "yes"}]') }
let(:rating_none_type) { 'none' }
let(:rating_none_name) { 'None' }
let(:rating_hash) do
JSON.parse('[{"type":"pointers","value":"4", "pavilion": "yes"}]')
end
let(:rating_none_type) { "none" }
let(:rating_none_name) { "None" }
fab!(:rating_category) { Fabricate(:category) }
fab!(:user) { sign_in(Fabricate(:user)) }
fab!(:rating_topic) { Fabricate(:topic, category: rating_category) }
fab!(:rating_post) { Fabricate(:post, topic: rating_topic, user: user) }
let(:none_rating_json) { '[{"type":"none","value":"4", "pavilion": "yes"}]' }
let(:multiple_rating_hash) { JSON.parse('[{"type":"pointers","value":"4", "pavilion": "yes"}, {"type":"handwriting","value":"3"}]') }
let(:multiple_rating_hash) do
JSON.parse(
'[{"type":"pointers","value":"4", "pavilion": "yes"}, {"type":"handwriting","value":"3"}]'
)
end
let(:create_params) do
{
raw: 'new body',
raw: "new body",
ratings: none_rating_json,
topic_id: rating_topic.id,
user_id: user.id
user_id: user.id,
}
end
let(:update_params) do
{
post: {
raw: 'edited body',
ratings: none_rating_json,
}
}
{ post: { raw: "edited body", ratings: none_rating_json } }
end
let(:update_params_missing) { { post: { raw: "edited" } } }
it "adds the the rating correctly" do
SiteSetting.rating_enabled = true

Category.any_instance.stubs(:rating_types).returns([rating_none_type])
post "/posts.json", params: create_params
expect(response.status).to eq(200)

post_id = JSON.parse(response.body)['id']
post_id = JSON.parse(response.body)["id"]
post = Post.find(post_id)
expect(post.custom_fields['rating_none']).to be_present
expect(post.custom_fields["rating_none"]).to be_present
end

it "updates the rating correctly" do
Expand All @@ -50,6 +52,13 @@
put "/posts/#{post.id}.json", params: update_params
expect(response.status).to eq(200)
post.reload
expect(post.custom_fields['rating_none']).to be_present
expect(post.custom_fields["rating_none"]).to be_present
end
it "errors when data is incorrect" do
post = Fabricate(:post, user: user)
put "/posts/#{post.id}.json", params: update_params_missing
expect(response.status).to eq(200)
post.reload
expect(post.custom_fields["rating_none"]).not_to be_present
end
end