From 8c812df99cd454b740a7d8526e225761a0b6a2fe Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Sun, 22 Mar 2020 14:22:10 +0200 Subject: [PATCH] Add some forum testing --- app/controllers/directories_controller.rb | 2 +- spec/controllers/about_controller_spec.rb | 40 +++++++++---------- spec/controllers/shoutmsgs_controller_spec.rb | 8 ++-- spec/factories/category.rb | 4 ++ spec/factories/forum.rb | 15 +++++++ spec/factories/post.rb | 4 ++ spec/factories/topic.rb | 10 +++++ spec/features/forums/read_forums_spec.rb | 36 +++++++++++++++++ spec/features/users/user_signs_up_spec.rb | 1 + 9 files changed, 96 insertions(+), 24 deletions(-) create mode 100644 spec/features/forums/read_forums_spec.rb diff --git a/app/controllers/directories_controller.rb b/app/controllers/directories_controller.rb index 677fa4b..2008329 100644 --- a/app/controllers/directories_controller.rb +++ b/app/controllers/directories_controller.rb @@ -6,7 +6,7 @@ class DirectoriesController < ApplicationController @files = @directory.files render partial: 'data_files/list', layout: true else - @directories = Directory.ordered.filtered.all conditions: { parent_id: 1 } + @directories = Directory.ordered.filtered.where(parent_id: 1) end end diff --git a/spec/controllers/about_controller_spec.rb b/spec/controllers/about_controller_spec.rb index dc4d9d1..495458b 100644 --- a/spec/controllers/about_controller_spec.rb +++ b/spec/controllers/about_controller_spec.rb @@ -1,26 +1,26 @@ require 'rails_helper' RSpec.describe AboutController, type: :controller do - it "renders the staff template" do - get :staff - expect(response).to render_template("staff") + it "renders the staff template" do + get :staff + expect(response).to render_template("staff") + end + + context 'as an admin' do + let!(:admin) { create(:user, :admin) } + + before do + login_admin end - - context 'as an admin' do - let!(:admin) { create(:user, :admin) } - - before do - login_admin - end - - it "renders the adminpanel template" do - get :adminpanel - expect(response).to render_template("adminpanel") - end - - it "renders the statistics template" do - get :statistics - expect(response).to render_template("statistics") - end + + it "renders the adminpanel template" do + get :adminpanel + expect(response).to render_template("adminpanel") end + + it "renders the statistics template" do + get :statistics + expect(response).to render_template("statistics") + end + end end diff --git a/spec/controllers/shoutmsgs_controller_spec.rb b/spec/controllers/shoutmsgs_controller_spec.rb index d6a8f5b..5a9dffb 100644 --- a/spec/controllers/shoutmsgs_controller_spec.rb +++ b/spec/controllers/shoutmsgs_controller_spec.rb @@ -1,9 +1,11 @@ require 'rails_helper' RSpec.describe ShoutmsgsController, type: :controller do + context 'GET #index' do it "renders the index template" do - get :index - expect(response).to have_http_status(200) - expect(response).to render_template("index") + get :index + expect(response).to have_http_status(200) + expect(response).to render_template("index") end + end end diff --git a/spec/factories/category.rb b/spec/factories/category.rb index c691b85..1ba464d 100755 --- a/spec/factories/category.rb +++ b/spec/factories/category.rb @@ -11,4 +11,8 @@ FactoryBot.define do trait :game do domain Category::DOMAIN_GAMES end + + trait :forums do + domain Category::DOMAIN_FORUMS + end end diff --git a/spec/factories/forum.rb b/spec/factories/forum.rb index fb75bf1..eec7adb 100644 --- a/spec/factories/forum.rb +++ b/spec/factories/forum.rb @@ -2,5 +2,20 @@ FactoryBot.define do factory :forum do sequence(:title) { |n| "Forum Title #{n}" } sequence(:description) { |n| "Forum Description #{n}" } + + before :create do |forum| + cat = create(:category, :forums) + forum.category = cat + end + + trait :with_content do + after :create do |forum| + (rand(30..100)).times do + topic = build :topic, :with_content + topic.forum = forum + topic.save + end + end + end end end diff --git a/spec/factories/post.rb b/spec/factories/post.rb index b01a210..dcecd2e 100644 --- a/spec/factories/post.rb +++ b/spec/factories/post.rb @@ -3,5 +3,9 @@ FactoryBot.define do sequence(:text) { |n| "Post Body #{n}" } topic user + + trait :with_content do + text { (0..100).map { (0...8).map { (65 + rand(26)).chr }.join }.join(" ") } + end end end diff --git a/spec/factories/topic.rb b/spec/factories/topic.rb index 3199272..318d110 100644 --- a/spec/factories/topic.rb +++ b/spec/factories/topic.rb @@ -6,5 +6,15 @@ FactoryBot.define do before(:create) do |topic| topic.first_post = "My first post on the topic" end + + trait :with_content do + after :create do |topic| + (rand(1..30)).times do + post = build :post + post.topic = topic + post.save + end + end + end end end diff --git a/spec/features/forums/read_forums_spec.rb b/spec/features/forums/read_forums_spec.rb new file mode 100644 index 0000000..a87351e --- /dev/null +++ b/spec/features/forums/read_forums_spec.rb @@ -0,0 +1,36 @@ +require 'rails_helper' + +feature 'User reads forums', js: :true do + before :all do + create_list(:forum, 5, :with_content) + end + + context 'as a basic user' do + let!(:user) { create(:user) } + + before do + sign_in_as(user) + end + + it 'has forum header' do + visit forums_path + expect(page).to have_selector("td.forum h5") + end + + #it 'has forum description' do + # visit forums_path + # expect("td.forum").to have_content() + #end + + it 'can click last post' do + find('td.last>a').click + expect(response).to have_http_status(200) + end + end + + private + + def long_text(len = 10_000) + (0..len).map{ (0...8).map { (65 + rand(26)).chr }.join }.join(" ") # 90008 + end +end diff --git a/spec/features/users/user_signs_up_spec.rb b/spec/features/users/user_signs_up_spec.rb index 6567bae..0dd5fe5 100755 --- a/spec/features/users/user_signs_up_spec.rb +++ b/spec/features/users/user_signs_up_spec.rb @@ -9,6 +9,7 @@ feature "Visitor signs up", js: :true do scenario "with valid Username, Email, Password and Steam ID" do within registration_form do + fill_form(:user, user.slice(*sign_up_attributes)) click_button submit(:user, :create) end