Fix more tests

This commit is contained in:
Ari Timonen 2020-03-18 23:50:26 +02:00
parent 5a2324166e
commit 4c5108bc4e
6 changed files with 182 additions and 33 deletions

View file

@ -16,18 +16,19 @@ class ArticlesController < ApplicationController
def admin
raise AccessError unless cuser and cuser.admin?
# FIXME: something better?
@articles = {"Drafts" => Article.drafts.ordered, "Special" => Article.category(Category::SPECIAL).ordered}
end
def show
raise AccessError unless @article.can_show? cuser
@article.mark_as_read! for: cuser if cuser
# OBSOLETE
# @article.record_view_count(request.remote_ip, cuser.nil?)
end
def new
@article = Article.new
@article.text_coding = Article::CODING_HTML
raise AccessError unless @article.can_create? cuser
end
@ -39,7 +40,7 @@ class ArticlesController < ApplicationController
end
def create
@article = Article.new Article.article_params(params, cuser)
@article = Article.new(Article.article_params(params, cuser))
@article.user = cuser
raise AccessError unless @article.can_create? cuser
@ -52,7 +53,7 @@ class ArticlesController < ApplicationController
end
def update
raise AccessError unless @article.can_update? cuser, Article.article_params(params, cuser)
raise AccessError unless @article.can_update?(cuser, Article.article_params(params, cuser))
if @article.update_attributes(Article.article_params(params, cuser))
flash[:notice] = t(:articles_update)
redirect_to @article
@ -61,6 +62,7 @@ class ArticlesController < ApplicationController
end
end
# TODO: link it somewhere
def cleanup
raise AccessError unless @article.can_update? cuser
@article.text = strip(@article.text)

View file

@ -41,6 +41,9 @@ class Article < ActiveRecord::Base
G_RULES = 464
COMPMOD = 998
attribute :text_coding, :integer, default: CODING_HTML
attribute :status, :integer, default: STATUS_DRAFT
scope :recent, -> { order('created_at DESC').limit(8) }
scope :with_comments, -> {
select("articles.*, COUNT(C.id) AS comment_num").
@ -101,8 +104,8 @@ class Article < ActiveRecord::Base
end
def init_variables
self.status = STATUS_DRAFT unless user.admin?
self.text_coding = CODING_BBCODE if !user.admin? and text_coding = CODING_HTML
self.status = STATUS_DRAFT unless user&.admin?
self.text_coding = CODING_BBCODE if (!user&.admin? and text_coding == CODING_HTML)
end
def format_text

View file

@ -1,23 +1,25 @@
<div class="wide box">
<h1>
Admin Menu
</h1>
<ul class="disc">
<li><%= link_to 'New Article', new_article_path %></li>
<li><%= link_to "Article Admin", controller: "articles", action: "admin" %></li>
<li><%= link_to "Files Admin", controller: "directories", action: "show", id: Directory.first %></li>
<li>
<%= link_to issues_path do %>
Issues (<%= Issue.with_status(0).count %>)
<% end %>
</li>
<li><%= link_to "Bans", bans_path %></li>
<li><%= link_to "Groups", groups_path %></li>
<li><%= link_to "Categories", categories_path %></li>
<li><%= link_to "Polls", polls_path %></li>
<li><%= link_to "Contests", contests_path %></li>
<li><%= link_to "Challenges", challenges_path %></li>
<li><%= link_to "Maps", maps_path %></li>
<li><%= link_to "Custom Article URLs", custom_urls_path %></li>
</ul>
</div>
<h1>
Admin Menu
</h1>
<ul class="disc">
<li><%= link_to 'New Article', new_article_path %></li>
<li><%= link_to "Article Admin", controller: "articles", action: "admin" %></li>
<% if Directory.first %>
<li><%= link_to "Files Admin", controller: "directories", action: "show", id: Directory.first %></li>
<% end %>
<li>
<%= link_to issues_path do %>
Issues (<%= Issue.with_status(0).count %>)
<% end %>
</li>
<li><%= link_to "Bans", bans_path %></li>
<li><%= link_to "Groups", groups_path %></li>
<li><%= link_to "Categories", categories_path %></li>
<li><%= link_to "Polls", polls_path %></li>
<li><%= link_to "Contests", contests_path %></li>
<li><%= link_to "Challenges", challenges_path %></li>
<li><%= link_to "Maps", maps_path %></li>
<li><%= link_to "Custom Article URLs", custom_urls_path %></li>
</ul>
</div>

View file

@ -3,5 +3,5 @@
<%= render partial: 'list', locals: { articles: @news } %>
<% if cuser and cuser.admin? %>
<%= link_to 'New Article', new_article_path, class: 'button' %>
<%= link_to 'New Article', new_article_path, class: 'button' %>
<% end %>

View file

@ -1,8 +1,150 @@
require 'rails_helper'
RSpec.describe ArticlesController, type: :controller do
it "renders the index template" do
get :index
expect(response).to render_template("index")
let!(:category) { create(:category, domain: Category::DOMAIN_NEWS) }
let!(:params) { FactoryBot.attributes_for(:article).merge!(category_id: category.id) }
let!(:invalid_params) { params.merge!(:title => (0..150).map { (65 + rand(26)).chr }.join) }
let!(:article) { create(:article, category_id: category.id, user_id: admin.id) }
let!(:admin) { create(:user, :admin) }
let!(:user) { create(:user) }
describe 'GET #index' do
it "renders the template" do
get :index
expect(response).to render_template("index")
end
it "assigns categories" do
get :index
expect(assigns(:categories)).to eq(Category.ordered.nospecial.domain Category::DOMAIN_ARTICLES)
end
end
describe 'GET #news_index' do
it "renders the news index" do
get :news_index
expect(response).to render_template("news_index")
end
end
describe 'GET #news_archive' do
it "renders the news archive" do
get :news_archive
expect(response).to render_template("news_archive")
end
end
describe 'GET #admin' do
it "renders the template" do
login_admin
get :admin
expect(response).to render_template("admin")
end
end
describe 'GET #edit' do
let!(:article) { create(:article, category_id: category.id, user_id: admin.id) }
it "renders the template" do
login_admin
get :edit, params: {id: article.id}
expect(response).to render_template("edit")
end
end
context 'POST' do
describe 'with valid values' do
it "creates the model" do
login_admin
post :create, params: {:article => params}
# Article.any_instance.should_receive(:update_attributes).with(params)
expect(Article.last).to have_attributes(params)
end
it "redirects correctly" do
login_admin
post :create, params: {:article => params}
expect(response).to redirect_to(article_path(Article.last))
end
end
describe 'with invalid values' do
it "does not create the model" do
login_admin
count = Article.count
post :create, params: {:article => invalid_params}
# Article.any_instance.should_receive(:update_attributes).with(params)
expect(Article.count).to eq(count)
end
it "redirects correctly" do
login_admin
post :create, params: {:article => invalid_params}
expect(response).to redirect_to(article_path(Article.last))
end
end
end
context 'PUT' do
describe 'with valid values' do
it "updates the model" do
login_admin
params = FactoryBot.attributes_for(:article).merge!(category_id: category.id)
put :update, params: {:id => article.id, :article => params}
# Article.any_instance.should_receive(:update_attributes).with(params)
expect(Article.find(article.id).attributes).not_to eq(article.attributes)
end
it "redirects correctly" do
login_admin
put :update, params: {:id => article.id, :article => params}
expect(response).to redirect_to(article_path(Article.last))
end
end
describe 'with invalid values' do
it "does not update the model" do
login_admin
put :update, params: {:id => article.id, :article => invalid_params}
expect(Article.find(article.id).attributes).to eq(article.attributes)
end
it "redirects correctly" do
login_admin
post :create, params: {:article => invalid_params}
expect(response).to redirect_to(article_path(Article.last))
end
end
end
context 'DELETE' do
describe 'with valid parameters' do
it "deletes the model" do
login_admin
count = Article.count
delete :destroy, params: {:id => article.id}
expect(Article.where(id: article.id).count).to eq(0)
expect(Article.count).to eq(count - 1)
# Article.any_instance.should_receive(:update_attributes).with(params)
end
it "redirects correctly" do
login_admin
delete :destroy, params: {:id => article.id}
expect(response).to redirect_to("where_i_came_from")
end
end
describe 'without access' do
it "does not delete the model" do
login(user.username)
count = Article.count
delete :destroy, params: {:id => article.id}
expect(Article.count).to eq(count)
end
end
end
end

View file

@ -15,7 +15,7 @@ feature 'User creates new article', js: :true do
it 'creates an article successfully' do
fill_in attribute_translation(:article, :title), with: article[:title]
fill_tinymce "#article_text", article[:text]
fill_tinymce "article_text", article[:text]
click_button I18n.t('helpers.submit.post.create')
expect(page).to have_content(I18n.t('articles_create'))
@ -23,7 +23,7 @@ feature 'User creates new article', js: :true do
it 'creates an article with a text length greater than 65535 bytes' do
fill_in attribute_translation(:article, :title), with: article[:title]
fill_tinymce "#article_text", long_text
fill_tinymce "article_text", long_text
click_button I18n.t('helpers.submit.post.create')
expect(page).to have_content(I18n.t('articles_create'))