mirror of
https://github.com/ENSL/ensl.org.git
synced 2025-01-14 13:51:26 +00:00
Fix more tests
This commit is contained in:
parent
5a2324166e
commit
4c5108bc4e
6 changed files with 182 additions and 33 deletions
|
@ -16,18 +16,19 @@ class ArticlesController < ApplicationController
|
||||||
|
|
||||||
def admin
|
def admin
|
||||||
raise AccessError unless cuser and cuser.admin?
|
raise AccessError unless cuser and cuser.admin?
|
||||||
|
# FIXME: something better?
|
||||||
@articles = {"Drafts" => Article.drafts.ordered, "Special" => Article.category(Category::SPECIAL).ordered}
|
@articles = {"Drafts" => Article.drafts.ordered, "Special" => Article.category(Category::SPECIAL).ordered}
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
raise AccessError unless @article.can_show? cuser
|
raise AccessError unless @article.can_show? cuser
|
||||||
@article.mark_as_read! for: cuser if cuser
|
@article.mark_as_read! for: cuser if cuser
|
||||||
|
# OBSOLETE
|
||||||
# @article.record_view_count(request.remote_ip, cuser.nil?)
|
# @article.record_view_count(request.remote_ip, cuser.nil?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@article = Article.new
|
@article = Article.new
|
||||||
@article.text_coding = Article::CODING_HTML
|
|
||||||
raise AccessError unless @article.can_create? cuser
|
raise AccessError unless @article.can_create? cuser
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,7 +40,7 @@ class ArticlesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@article = Article.new Article.article_params(params, cuser)
|
@article = Article.new(Article.article_params(params, cuser))
|
||||||
@article.user = cuser
|
@article.user = cuser
|
||||||
raise AccessError unless @article.can_create? cuser
|
raise AccessError unless @article.can_create? cuser
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ class ArticlesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
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))
|
if @article.update_attributes(Article.article_params(params, cuser))
|
||||||
flash[:notice] = t(:articles_update)
|
flash[:notice] = t(:articles_update)
|
||||||
redirect_to @article
|
redirect_to @article
|
||||||
|
@ -61,6 +62,7 @@ class ArticlesController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: link it somewhere
|
||||||
def cleanup
|
def cleanup
|
||||||
raise AccessError unless @article.can_update? cuser
|
raise AccessError unless @article.can_update? cuser
|
||||||
@article.text = strip(@article.text)
|
@article.text = strip(@article.text)
|
||||||
|
|
|
@ -41,6 +41,9 @@ class Article < ActiveRecord::Base
|
||||||
G_RULES = 464
|
G_RULES = 464
|
||||||
COMPMOD = 998
|
COMPMOD = 998
|
||||||
|
|
||||||
|
attribute :text_coding, :integer, default: CODING_HTML
|
||||||
|
attribute :status, :integer, default: STATUS_DRAFT
|
||||||
|
|
||||||
scope :recent, -> { order('created_at DESC').limit(8) }
|
scope :recent, -> { order('created_at DESC').limit(8) }
|
||||||
scope :with_comments, -> {
|
scope :with_comments, -> {
|
||||||
select("articles.*, COUNT(C.id) AS comment_num").
|
select("articles.*, COUNT(C.id) AS comment_num").
|
||||||
|
@ -101,8 +104,8 @@ class Article < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def init_variables
|
def init_variables
|
||||||
self.status = STATUS_DRAFT unless user.admin?
|
self.status = STATUS_DRAFT unless user&.admin?
|
||||||
self.text_coding = CODING_BBCODE if !user.admin? and text_coding = CODING_HTML
|
self.text_coding = CODING_BBCODE if (!user&.admin? and text_coding == CODING_HTML)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_text
|
def format_text
|
||||||
|
|
|
@ -5,7 +5,9 @@
|
||||||
<ul class="disc">
|
<ul class="disc">
|
||||||
<li><%= link_to 'New Article', new_article_path %></li>
|
<li><%= link_to 'New Article', new_article_path %></li>
|
||||||
<li><%= link_to "Article Admin", controller: "articles", action: "admin" %></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>
|
<li><%= link_to "Files Admin", controller: "directories", action: "show", id: Directory.first %></li>
|
||||||
|
<% end %>
|
||||||
<li>
|
<li>
|
||||||
<%= link_to issues_path do %>
|
<%= link_to issues_path do %>
|
||||||
Issues (<%= Issue.with_status(0).count %>)
|
Issues (<%= Issue.with_status(0).count %>)
|
||||||
|
|
|
@ -1,8 +1,150 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe ArticlesController, type: :controller do
|
RSpec.describe ArticlesController, type: :controller do
|
||||||
it "renders the index template" do
|
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
|
get :index
|
||||||
expect(response).to render_template("index")
|
expect(response).to render_template("index")
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -15,7 +15,7 @@ feature 'User creates new article', js: :true do
|
||||||
|
|
||||||
it 'creates an article successfully' do
|
it 'creates an article successfully' do
|
||||||
fill_in attribute_translation(:article, :title), with: article[:title]
|
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')
|
click_button I18n.t('helpers.submit.post.create')
|
||||||
|
|
||||||
expect(page).to have_content(I18n.t('articles_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
|
it 'creates an article with a text length greater than 65535 bytes' do
|
||||||
fill_in attribute_translation(:article, :title), with: article[:title]
|
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')
|
click_button I18n.t('helpers.submit.post.create')
|
||||||
|
|
||||||
expect(page).to have_content(I18n.t('articles_create'))
|
expect(page).to have_content(I18n.t('articles_create'))
|
||||||
|
|
Loading…
Reference in a new issue