diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 4a259e2..3e63006 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -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) diff --git a/app/models/article.rb b/app/models/article.rb index 20f7e54..783746c 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/views/about/adminpanel.html.erb b/app/views/about/adminpanel.html.erb index f4b0099..783df4d 100644 --- a/app/views/about/adminpanel.html.erb +++ b/app/views/about/adminpanel.html.erb @@ -1,23 +1,25 @@
-

- Admin Menu -

- -
+

+ Admin Menu +

+ + \ No newline at end of file diff --git a/app/views/articles/news_archive.html.erb b/app/views/articles/news_archive.html.erb index e3eb825..915a203 100644 --- a/app/views/articles/news_archive.html.erb +++ b/app/views/articles/news_archive.html.erb @@ -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 %> \ No newline at end of file diff --git a/spec/controllers/articles_controller_spec.rb b/spec/controllers/articles_controller_spec.rb index 41623e3..e2840dc 100644 --- a/spec/controllers/articles_controller_spec.rb +++ b/spec/controllers/articles_controller_spec.rb @@ -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 diff --git a/spec/features/articles/new_article_spec.rb b/spec/features/articles/new_article_spec.rb index 0bec90a..b47b471 100644 --- a/spec/features/articles/new_article_spec.rb +++ b/spec/features/articles/new_article_spec.rb @@ -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'))