Browse Source

Category unit tests

pull/44/head
Stephen 6 months ago
parent
commit
e168c2eaa7
10 changed files with 456 additions and 21 deletions
  1. +2
    -2
      app/controllers/application_controller.rb
  2. +12
    -4
      app/controllers/category_controller.rb
  3. +2
    -2
      app/controllers/post_controller.rb
  4. +1
    -0
      app/models/category.rb
  5. +117
    -1
      db/structure.sql
  6. +242
    -3
      test/controllers/category_controller_test.rb
  7. +5
    -3
      test/fixtures/categories.yml
  8. +6
    -0
      test/fixtures/posts.yml
  9. +62
    -3
      test/models/category_test.rb
  10. +7
    -3
      test/models/post_test.rb

+ 2
- 2
app/controllers/application_controller.rb View File

@@ -104,8 +104,8 @@ html
flash[:alert] = "Please login first."
redirect_to login_path
elsif get_user_blog.nil? || !get_user_blog.can_edit(current_user)
flash[:alert] = "You don't have access to that page!"
redirect_back fallback_location: root_path
flash[:alert] = "You do not have access to that page!"
render file: "#{Rails.root}/public/403.html" , status: :forbidden
end
end



+ 12
- 4
app/controllers/category_controller.rb View File

@@ -11,7 +11,7 @@ class CategoryController < ApplicationController
render file: "#{Rails.root}/public/404.html" , status: :not_found
return
end
@posts = @cat.posts
@posts = @cat.posts.where(is_draft: false).all
end

def manage
@@ -37,14 +37,22 @@ class CategoryController < ApplicationController
@blog = get_user_blog
@cats = @blog.categories
c = @cats.find_by_path(params[:cat_name])
c.destroy unless c.nil?
render :manage
if c.nil?
flash[:alert] = "Could not find that category!"
else
c.destroy
end
redirect_to manage_category_path
end

def update
blog = get_user_blog
c = blog.categories.find_by_path(params[:cat_name])
c.update(params[:category].permit(:name))
if c.nil?
flash[:alert] = "Could not find that category!"
else
c.update(params[:category].permit(:name))
end
redirect_to manage_category_path
end
end

+ 2
- 2
app/controllers/post_controller.rb View File

@@ -83,7 +83,7 @@ class PostController < ApplicationController
post = blog.posts.create(title: params[:post][:title],
body: params[:post][:body],
is_draft: params[:post][:is_draft] || false)
cats = params[:categories]
cats = params[:categories] || []
cats.each do |k, v|
c = blog.categories.find_by_path(k)
unless c.nil?
@@ -119,7 +119,7 @@ class PostController < ApplicationController
@post.update(post_params)
@post.save

cats = params[:categories]
cats = params[:categories] || []
cats.each do |k, v|
c = @post.blog.categories.find_by_path(k)
unless c.nil?


+ 1
- 0
app/models/category.rb View File

@@ -10,6 +10,7 @@ class Category < ApplicationRecord
before_validation :set_path

def set_path
return if self.name.nil?
self.path = self.name.parameterize
end


+ 117
- 1
db/structure.sql View File

@@ -64,6 +64,49 @@ ALTER SEQUENCE public.blogs_id_seq OWNED BY public.blogs.id;


--
-- Name: categories; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.categories (
id bigint NOT NULL,
name character varying NOT NULL,
path character varying NOT NULL,
blog_id bigint NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);


--
-- Name: categories_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.categories_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: categories_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.categories_id_seq OWNED BY public.categories.id;


--
-- Name: categories_posts; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.categories_posts (
category_id bigint NOT NULL,
post_id bigint NOT NULL
);


--
-- Name: comments; Type: TABLE; Schema: public; Owner: -
--

@@ -365,6 +408,13 @@ ALTER TABLE ONLY public.blogs ALTER COLUMN id SET DEFAULT nextval('public.blogs_


--
-- Name: categories id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.categories ALTER COLUMN id SET DEFAULT nextval('public.categories_id_seq'::regclass);


--
-- Name: comments id; Type: DEFAULT; Schema: public; Owner: -
--

@@ -437,6 +487,14 @@ ALTER TABLE ONLY public.blogs


--
-- Name: categories categories_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.categories
ADD CONSTRAINT categories_pkey PRIMARY KEY (id);


--
-- Name: comments comments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

@@ -530,6 +588,55 @@ CREATE INDEX index_blogs_on_user_id ON public.blogs USING btree (user_id);


--
-- Name: index_categories_on_blog_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_categories_on_blog_id ON public.categories USING btree (blog_id);


--
-- Name: index_categories_on_name; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_categories_on_name ON public.categories USING btree (name);


--
-- Name: index_categories_on_name_and_blog_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_categories_on_name_and_blog_id ON public.categories USING btree (name, blog_id);


--
-- Name: index_categories_on_path; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_categories_on_path ON public.categories USING btree (path);


--
-- Name: index_categories_on_path_and_blog_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_categories_on_path_and_blog_id ON public.categories USING btree (path, blog_id);


--
-- Name: index_categories_posts_on_category_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_categories_posts_on_category_id ON public.categories_posts USING btree (category_id);


--
-- Name: index_categories_posts_on_post_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_categories_posts_on_post_id ON public.categories_posts USING btree (post_id);


--
-- Name: index_comments_on_post_id; Type: INDEX; Schema: public; Owner: -
--

@@ -586,6 +693,14 @@ CREATE INDEX index_video_meta_on_user_id ON public.video_meta USING btree (user_


--
-- Name: categories fk_rails_962c9e7346; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.categories
ADD CONSTRAINT fk_rails_962c9e7346 FOREIGN KEY (blog_id) REFERENCES public.blogs(id);


--
-- Name: pages fk_rails_fe96962485; Type: FK CONSTRAINT; Schema: public; Owner: -
--

@@ -623,6 +738,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191207213541'),
('20191212184641'),
('20200126015317'),
('20200204151117');
('20200204151117'),
('20200322145832');



+ 242
- 3
test/controllers/category_controller_test.rb View File

@@ -1,7 +1,246 @@
require 'test_helper'

class CategoryControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
test "GET fake category returns 404" do
get category_path(blog_name: blogs(:one).urlName,
cat_name: "fake")
assert_response :not_found
end

test "GET category with fake blog name returns 404" do
get category_path(blog_name: "fake",
cat_name: categories(:one).path)
assert_response :not_found
end

test "GET category with wrong blog name returns 404" do
get category_path(blog_name: blogs(:two).urlName,
cat_name: categories(:one).path)
assert_response :not_found
end

test "GET category with no posts returns empty" do
get category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:two).path)
assert_response :success
assert_equal 0, assigns(:posts).count
end

test "GET category with multiple posts returns them" do
c = categories(:one)
c.posts << posts(:one)
c.posts << posts(:three)
get category_path(c.to_param)
assert_response :success
assert_equal [posts(:three), posts(:one)], c.posts
end

test "GET category doesn't return draft posts" do
c = categories(:one)
c.posts << posts(:draft)
get category_path(c.to_param)
assert_response :success
assert_equal 0, assigns(:posts).count
end

test "MANAGE redirects to login when not logged in" do
get manage_category_path(blog_name: blogs(:one).urlName)
assert_redirected_to login_path
assert_equal "Please login first.", flash[:alert]
end

test "MANAGE returns 403 when logged in as other user" do
post login_path(login: {
username: "user_one",
password: "password1"
})
get manage_category_path(blog_name: blogs(:two).urlName)
assert_response :forbidden
assert_equal "You do not have access to that page!", flash[:alert]
end

test "MANAGE returns 403 when blog doesn't exist" do
# If we returned 404, it would be possible to use a brute force attack
# to find blogs belonging to other users
post login_path(login: {
username: "user_one",
password: "password1"
})
get manage_category_path(blog_name: "definitely_dne")
assert_response :forbidden
assert_equal "You do not have access to that page!", flash[:alert]
end

test "MANAGE returns 200 when user is logged in to correct account" do
post login_path(login: {
username: "user_one",
password: "password1"
})
get manage_category_path(blog_name: blogs(:one).urlName)
assert_response :success
end

test "CREATE redirects to login when not logged in" do
post manage_category_path(blog_name: blogs(:one).urlName,
category: {name: "test category"})
assert_redirected_to login_path
assert_nil Category.find_by_name "test category"
end

test "CREATE returns 403 when logged in as other user" do
post login_path(login: {
username: "user_one",
password: "password1"
})
post manage_category_path(blog_name: blogs(:two).urlName,
category: {name: "test category"})
assert_response :forbidden
assert_nil Category.find_by_name "test category"
end

test "CREATE returns 403 when blog doesn't exist" do
post login_path(login: {
username: "user_one",
password: "password1"
})
post manage_category_path(blog_name: "definitely_dne",
category: {name: "test category"})
assert_response :forbidden
assert_nil Category.find_by_name "test category"
end

test "CREATE flashes alerts on error" do
post login_path(login: {
username: "user_one",
password: "password1"
})
# Name too short
post manage_category_path(blog_name: blogs(:one).urlName,
category: {name: "hi"})
assert_response :success
assert_equal ["Name is too short (minimum is 3 characters)"],
flash[:alerts]
assert_nil Category.find_by_name "hi"
end

test "CREATE returns 200 when user is logged in correctly" do
post login_path(login: {
username: "user_one",
password: "password1"
})
post manage_category_path(blog_name: blogs(:one).urlName,
category: {name: "test category"})
assert_response :success
assert_not_nil blogs(:one).categories.find_by_name "test category"
end

test "DELETE redirects to login when not logged in" do
delete delete_category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:one).path)
assert_redirected_to login_path
assert_not_nil Category.find(categories(:one).id)
end

test "DELETE returns 403 when logged in as other user" do
post login_path(login: {
username: "user_two",
password: "password2"
})
delete delete_category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:one).path)
assert_response :forbidden
assert_not_nil Category.find(categories(:one).id)
end

test "DELETE returns 403 when blog doesn't exist" do
post login_path(login: {
username: "user_one",
password: "password1"
})
delete delete_category_path(blog_name: "dne",
cat_name: categories(:one).path)
assert_response :forbidden
assert_not_nil Category.find_by(id: categories(:one).id)
end

test "DELETE redirects to manage when category doesn't exist" do
post login_path(login: {
username: "user_one",
password: "password1"
})
delete delete_category_path(blog_name: blogs(:one).urlName,
cat_name: "dne")
assert_redirected_to manage_category_path
assert_equal "Could not find that category!", flash[:alert]
end

test "DELETE returns 200 otherwise" do
post login_path(login: {
username: "user_one",
password: "password1"
})
delete delete_category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:one).path)
assert_redirected_to manage_category_path
assert_nil Category.find_by(id: categories(:one).id)
end

test "UPDATE redirects to login when not logged in" do
og_name = categories(:one).name
patch update_category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:one).path,
category: {name: "new name"})
assert_redirected_to login_path
assert_equal og_name, Category.find(categories(:one).id).name
end

test "UPDATE returns 403 when logged in as other user" do
og_name = categories(:one).name
post login_path(login: {
username: "user_two",
password: "password2"
})
patch update_category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:one).path,
category: {name: "new name"})
assert_response :forbidden
assert_equal og_name, Category.find(categories(:one).id).name
end

test "UPDATE returns 403 when blog doesn't exist" do
og_name = categories(:one).name
post login_path(login: {
username: "user_one",
password: "password1"
})
patch update_category_path(blog_name: "dne",
cat_name: categories(:one).path,
category: {name: "new name"})
assert_response :forbidden
assert_equal og_name, Category.find(categories(:one).id).name
end

test "UPDATE redirects to manage when category doesn't exist" do
post login_path(login: {
username: "user_one",
password: "password1"
})
patch update_category_path(blog_name: blogs(:one).urlName,
cat_name: "dne",
category: {name: "new name"})
assert_redirected_to manage_category_path
assert_equal "Could not find that category!", flash[:alert]
end

test "UPDATE returns 200 otherwise" do
post login_path(login: {
username: "user_one",
password: "password1"
})
patch update_category_path(blog_name: blogs(:one).urlName,
cat_name: categories(:one).path,
category: {name: "new name"})
assert_redirected_to manage_category_path
assert_equal "new name", Category.find(categories(:one).id).name
end
end

+ 5
- 3
test/fixtures/categories.yml View File

@@ -1,9 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
name: my first category
path: my-first-category
blog: one

two:
name: MyString
blog: two
name: my second category
path: my-second-category
blog: one

+ 6
- 0
test/fixtures/posts.yml View File

@@ -12,6 +12,12 @@ two:
is_draft: false
blog: two

three:
title: third post
body: mybody
is_draft: false
blog: one

draft:
title: My draft post
body: This is a draft post


+ 62
- 3
test/models/category_test.rb View File

@@ -1,7 +1,66 @@
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "duplicate post/category pairings only returns one post" do
p = posts(:one)
c = categories(:one)
p.categories << c
p.categories << c
assert_equal 1, c.posts.count
end

test "can't create category with duplicate name" do
c = categories(:one)
c2 = Category.new
c2.name = c.name
c2.blog_id = c.blog_id
assert_not c2.save
assert_equal [[:name, "has already been taken"],
[:path, "has already been taken"]],
c2.errors.uniq
end

test "can't create category with duplicate path" do
c = categories(:one)
c2 = Category.new
c2.name = c.name + "!"
c2.blog_id = c.blog_id
assert_not c2.save
assert_equal [[:path, "has already been taken"]],
c2.errors.uniq
end

test "can't create category with short name" do
c = Category.new
c.name = "hi"
c.blog = blogs(:one)
assert_not c.save
assert_equal ["Name is too short (minimum is 3 characters)"],
c.errors.full_messages
end

test "can't create category with long name" do
c = Category.new
c.name = "1234567890123456789012345678901"
c.blog = blogs(:one)
assert_not c.save
assert_equal ["Name is too long (maximum is 30 characters)"],
c.errors.full_messages
end

test "can't create category with no name" do
c = Category.new
c.blog = blogs(:one)
assert_not c.save
assert_equal ["Name is too short (minimum is 3 characters)"],
c.errors.full_messages
end

test "can create category otherwise" do
c = Category.new
c.blog = blogs(:one)
c.name = "new category"
assert c.save
assert_equal "new-category", c.path
end
end

+ 7
- 3
test/models/post_test.rb View File

@@ -1,7 +1,11 @@
require 'test_helper'

class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "duplicate post/category pairings only returns one category" do
p = posts(:one)
c = categories(:one)
c.posts << p
c.posts << p
assert_equal 1, p.categories.count
end
end

Loading…
Cancel
Save