diff --git a/Guardfile b/Guardfile index 742a768..02c716c 100644 --- a/Guardfile +++ b/Guardfile @@ -18,8 +18,7 @@ guard :rspec, cmd: 'spring rspec' do watch('spec/spec_helper.rb') { "spec" } # Rails example - watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } - watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } + watch(%r{^app/([^\.]+).*?(\.haml|\.rb)$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } watch(%r{^spec/support/(.+)\.rb$}) { "spec" } watch('config/routes.rb') { "spec/routing" } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b58981..bc8f68e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,6 +8,7 @@ class ApplicationController < ActionController::Base rescue_from CanCan::AccessDenied, with: :authentication_error rescue_from ActiveRecord::RecordNotFound, with: :not_found rescue_from ActionController::ParameterMissing, with: :wrong_parameters_error + rescue_from ActiveRecord::RecordInvalid, with: :wrong_parameters_error before_action :configure_devise_permitted_parameters, if: :devise_controller? diff --git a/app/controllers/forum_validations_controller.rb b/app/controllers/forum_validations_controller.rb index f5f7697..588634d 100644 --- a/app/controllers/forum_validations_controller.rb +++ b/app/controllers/forum_validations_controller.rb @@ -1,2 +1,33 @@ class ForumValidationsController < ApplicationController + def create + @forum_validation = ForumValidation.new resource_params + @forum_validation.save! # server error if it fails, shouldn't happen + redirect_to @forum_validation + end + + def update + @forum_validation = ForumValidation.find params[:id] + if @forum_validation.vid == params[:vid] + if @forum_validation.validated? + flash[:notice] = I18n.t('forum_validations.flash.update.already_success') + else + @forum_validation.associate_user_and_author! + @forum_validation.user.give_ownership_of_authored! + flash[:notice] = I18n.t('forum_validations.flash.update.success') + end + redirect_to @forum_validation + else + wrong_parameters_error + end + end + + def show + + end + + private + + def resource_params + params.require(:forum_validation).permit(:author_id, :user_id) + end end diff --git a/app/views/forum_validations/show.html.haml b/app/views/forum_validations/show.html.haml new file mode 100644 index 0000000..784e157 --- /dev/null +++ b/app/views/forum_validations/show.html.haml @@ -0,0 +1,12 @@ +%h1 Forum account validation + +- if @forum_validation.validated? + = t '.validated' +- else + = raw t '.not_validated' + +%h2 Mods associated: + +- @forum_validation.author.mods.each do |mod| + = link_to mod.name, mod + = link_to '[Edit]', [:edit, mod] diff --git a/config/locales/forum_validations.en.yml b/config/locales/forum_validations.en.yml new file mode 100644 index 0000000..0fbb25a --- /dev/null +++ b/config/locales/forum_validations.en.yml @@ -0,0 +1,17 @@ +en: + forum_validations: + flash: + update: + success: "You account have been successfully linked to your forum account! You can now manage all the mods!" + already_success: "You account has already been validated" + + show: + validated: | + Your forum account has been validated! + These are the mods that were associated with that account, you can now + manage them yourself: + not_validated: | + Your forum account hasn't been validated yet, please + check your PMs inbox + and click the link we sent you. + After you validate the forum account, you'll be able to manage all these mods associated with it: diff --git a/config/routes.rb b/config/routes.rb index f9ce162..d3e7f42 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,4 @@ Rails.application.routes.draw do - resources :forum_validations, except: [:new, :edit] - namespace :api, path: '/', constraints: { subdomain: 'api' } do resources :mods, only: [:index, :show] resources :categories, only: [:index, :show] do @@ -87,6 +85,12 @@ Rails.application.routes.draw do resources :mods, path: '/', only: :index end + resources :forum_validations, path: 'forum-validations', only: [:show, :create] do + member do + get '/validate', to: 'forum_validations#update' + end + end + get '/how-to-submit' => 'static#how_to_submit', as: :how_to_submit_static get '/how-to-install' => 'static#how_to_install', as: :how_to_install_static get '/how-to-make' => 'static#how_to_make', as: :how_to_make_static diff --git a/spec/controllers/forum_validations_controller_spec.rb b/spec/controllers/forum_validations_controller_spec.rb index af11841..a0ef99e 100644 --- a/spec/controllers/forum_validations_controller_spec.rb +++ b/spec/controllers/forum_validations_controller_spec.rb @@ -1,5 +1,120 @@ -require 'rails_helper' +describe ForumValidationsController, type: :controller do + describe 'POST create' do + it 'should create a new ForumValidation with the corresponding author and user' do + user = create :user + author = create :author + post :create, forum_validation: { user_id: user.id, author_id: author.id } + fv = ForumValidation.first + expect(fv.user).to eq user + expect(fv.author).to eq author + expect(fv.validated).to eq false + expect(fv.vid.size).to eq 50 + expect(response).to redirect_to fv + end -RSpec.describe ForumValidationsController, type: :controller do + it 'should throw a server error with invalid parameters' do + post :create, forum_validation: { user_id: 1234, author_id: 4321 } + expect(response).to have_http_status :bad_request + end + end + # Yes, GET + describe 'GET update' do + before(:each) do + @user = create :user + @author = create :author + create :mod, authors: [@author], owner: nil + create :mod, authors: [@author], owner: nil + @fv = create :forum_validation, user: @user, author: @author + end + + context 'with the correct #vid' do + before(:each) do + get :update, id: @fv.id, vid: @fv.vid + @fv.reload + @user.reload + @author.reload + end + + it 'should set the fv to validated' do + expect(@fv.validated).to eq true + end + + it 'should associate the user and author' do + expect(@user.author).to eq @author + expect(@author.user).to eq @user + end + + it 'should redirect to the fv' do + expect(response).to redirect_to @fv + end + + it 'should set the corresponding flash message' do + expect(flash[:notice]).to eq I18n.t('forum_validations.flash.update.success') + end + + it 'should set the ownership of all the mods associated with the author to the user' do + expect(@author.mods[0].owner).to eq @user + expect(@author.mods[1].owner).to eq @user + end + end + + describe 'with the incorrect #vid' do + before(:each) do + get :update, id: @fv.id, vid: 'rsniahsraeitn' + @fv.reload + @user.reload + @author.reload + end + + it 'should still be #validated false' do + expect(@fv.validated).to eq false + end + + it 'should NOT associate the user and author' do + expect(@user.author).to eq nil + expect(@author.user).to eq nil + end + + it 'throw a server error' do + expect(response).to have_http_status :bad_request + end + + it 'should not set ownership of the mods associated with the author to the user' do + expect(@author.mods[0].owner).to eq nil + expect(@author.mods[1].owner).to eq nil + end + end + + describe 'with an already validated ForumValidation' do + before(:each) do + @fv.associate_user_and_author! + @fv.user.give_ownership_of_authored! + get :update, id: @fv.id, vid: @fv.vid + @fv.reload + @user.reload + @author.reload + end + + it 'should redirect the user to the fv' do + expect(response).to redirect_to @fv + end + + it 'should set a different flash message' do + expect(flash[:notice]).to eq I18n.t('forum_validations.flash.update.already_success') + end + end + end + + describe 'GET show' do + it 'should render the show template' do + @fv = create :forum_validation + @m1 = create :mod, authors: [@fv.author] + @m2 = create :mod, authors: [@fv.author] + @fv.associate_user_and_author! + @fv.user.give_ownership_of_authored! + get :show, id: @fv.id + expect(response).to render_template :show + end + end end diff --git a/spec/models/forum_validation_spec.rb b/spec/models/forum_validation_spec.rb index c9bbe1c..dcd041a 100644 --- a/spec/models/forum_validation_spec.rb +++ b/spec/models/forum_validation_spec.rb @@ -43,4 +43,16 @@ describe ForumValidation, type: :model do expect(fv.validated).to eq true end end + + # describe '#send_pm' do + # def send_pm + # VCR.use_cassette('forum_validation', record: :new_episodes) do + # subject.send_pm + # end + # end + # + # it 'should authenticate and send a PM in the forum' do + # + # end + # end end diff --git a/spec/views/forum_validations/show_spec.rb b/spec/views/forum_validations/show_spec.rb new file mode 100644 index 0000000..55fd8f0 --- /dev/null +++ b/spec/views/forum_validations/show_spec.rb @@ -0,0 +1,41 @@ +describe 'forum_validations/show', type: :view do + before :each do + @fv = create :forum_validation + create :mod, authors: [@fv.author], name: 'Potato' + create :mod, authors: [@fv.author], name: 'Salad' + create :mod, authors: [@fv.author], name: 'Simulator' + end + + it 'display the list of mods associated with the author' do + assign(:forum_validation, @fv) + render + expect(rendered).to match(/Potato.*Salad.*Simulator/m) + end + + context '#validated = false' do + it 'should display a message indicating that the user is not validated' do + @fv.validated = false + assign(:forum_validation, @fv) + render + expect(rendered).to include I18n.t('forum_validations.show.not_validated') + end + end + + context '#validated = true' do + it 'should display a message indicating that the user is validated' do + @fv.validated = true + assign(:forum_validation, @fv) + render + expect(rendered).to include I18n.t('forum_validations.show.validated') + end + + it 'should display links to edit each the mods' do + @fv.validated = true + assign(:forum_validation, @fv) + render + expect(rendered).to have_link('', href: edit_mod_path(id: 'potato')) + expect(rendered).to have_link('', href: edit_mod_path(id: 'salad')) + expect(rendered).to have_link('', href: edit_mod_path(id: 'simulator')) + end + end +end