From ec264bf802c3cee261b9e594f2b474b09be673a1 Mon Sep 17 00:00:00 2001 From: Mac Siri Date: Tue, 19 Jun 2018 16:27:15 -0400 Subject: [PATCH] Clean up spec outputs (#450) * Improve BadgeAchievementSpec * Create specs for PushNotificationSubscription * Clean-up specs for VideoStatesController * Refactor spec --- app/controllers/video_states_controller.rb | 19 ++++++++---------- spec/factories/badge_achievements.rb | 8 ++++++++ spec/models/badge_achievement_spec.rb | 20 ++++--------------- .../push_notification_subscription_spec.rb | 20 +++++++++++++++++-- spec/requests/video_states_update_spec.rb | 20 +++++++++---------- 5 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 spec/factories/badge_achievements.rb diff --git a/app/controllers/video_states_controller.rb b/app/controllers/video_states_controller.rb index 2c1694a1f..dd006dcb9 100644 --- a/app/controllers/video_states_controller.rb +++ b/app/controllers/video_states_controller.rb @@ -1,23 +1,21 @@ class VideoStatesController < ApplicationController skip_before_action :verify_authenticity_token + def create unless valid_user - render json: { message: "invalid_user" }, :status => 422 + render json: { message: "invalid_user" }, status: 422 return end begin logger.info "VIDEO STATES: #{params}" - request_json = JSON.parse(request.raw_post, {symbolize_names: true}) + request_json = JSON.parse(request.raw_post, symbolize_names: true) logger.info "VIDEO STATES: #{request_json}" - rescue + rescue StandardError end - request_json = JSON.parse(request.raw_post, {symbolize_names: true}) - puts request_json - message_json = JSON.parse(request_json[:Message], {symbolize_names: true}) - puts message_json - puts "***" + request_json = JSON.parse(request.raw_post, symbolize_names: true) + message_json = JSON.parse(request_json[:Message], symbolize_names: true) @article = Article.find_by_video_code(message_json[:input][:key]) - @article.update(video_state: "COMPLETED") #Only is called on completion + @article.update(video_state: "COMPLETED") # Only is called on completion NotifyMailer.video_upload_complete_email(@article).deliver render json: { message: "Video state updated" } end @@ -27,5 +25,4 @@ class VideoStatesController < ApplicationController user = nil if !user.has_role?(:super_admin) user end - -end \ No newline at end of file +end diff --git a/spec/factories/badge_achievements.rb b/spec/factories/badge_achievements.rb new file mode 100644 index 000000000..f4b867852 --- /dev/null +++ b/spec/factories/badge_achievements.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :badge_achievement do + user + badge + rewarder { user } + rewarding_context_message_markdown { "Hello [Yoho](/hey)" } + end +end diff --git a/spec/models/badge_achievement_spec.rb b/spec/models/badge_achievement_spec.rb index bc1ae84d0..931ddccfa 100644 --- a/spec/models/badge_achievement_spec.rb +++ b/spec/models/badge_achievement_spec.rb @@ -1,29 +1,17 @@ require "rails_helper" RSpec.describe BadgeAchievement, type: :model do - let(:user) { create(:user) } - let(:badge) { create(:badge) } - describe "validations" do - subject { BadgeAchievement.create(user_id: user.id, badge_id: badge.id) } + subject { create(:badge_achievement) } it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:badge) } it { is_expected.to belong_to(:rewarder).class_name("User") } - end - - it "disallow duplicate badges" do - BadgeAchievement.create(user_id: user.id, badge_id: badge.id, rewarder_id: create(:user).id) - expect do - BadgeAchievement.create!(user_id: user.id, badge_id: badge.id, rewarder_id: create(:user).id) - end.to raise_error + it { is_expected.to validate_uniqueness_of(:badge_id).scoped_to(:user_id) } end it "turns rewarding_context_message_markdown into rewarding_context_message HTML" do - achievement = BadgeAchievement.create(user_id: user.id, - badge_id: badge.id, - rewarder_id: create(:user).id, - rewarding_context_message_markdown: "Hello [Yoho](/hey)") - expect(achievement.rewarding_context_message.include?("")) + achievement = create(:badge_achievement) + expect(achievement.rewarding_context_message).to include("") end end diff --git a/spec/models/push_notification_subscription_spec.rb b/spec/models/push_notification_subscription_spec.rb index 7bf033ffe..fe4473d87 100644 --- a/spec/models/push_notification_subscription_spec.rb +++ b/spec/models/push_notification_subscription_spec.rb @@ -1,5 +1,21 @@ -require 'rails_helper' +require "rails_helper" RSpec.describe PushNotificationSubscription, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe "validation" do + subject do + PushNotificationSubscription.create(user: create(:user), + auth_key: "asdf123", + endpoint: "asdf123") + end + + it { is_expected.to validate_presence_of(:endpoint) } + it { is_expected.to validate_presence_of(:user_id) } + it { is_expected.to validate_presence_of(:p256dh_key) } + it { is_expected.to validate_presence_of(:auth_key) } + it { is_expected.to validate_presence_of(:notification_type) } + it { is_expected.to validate_uniqueness_of(:endpoint) } + it { is_expected.to validate_uniqueness_of(:p256dh_key) } + it { is_expected.to validate_uniqueness_of(:auth_key) } + it { is_expected.to belong_to(:user) } + end end diff --git a/spec/requests/video_states_update_spec.rb b/spec/requests/video_states_update_spec.rb index ba8f9e88d..59696eea0 100644 --- a/spec/requests/video_states_update_spec.rb +++ b/spec/requests/video_states_update_spec.rb @@ -2,21 +2,21 @@ require "rails_helper" RSpec.describe "VideoStatesUpdate", type: :request do - before do - @user = FactoryBot.create(:user) - @user.update(secret:"TEST_SECRET") - @user.add_role(:super_admin) - @article = FactoryBot.create(:article, video_code: "DUMMY_VID_CODE") - end + let(:authorized_user) { create(:user, :super_admin, secret: "TEST_SECRET") } + let(:regular_user) { create(:user, secret: "TEST_SECRET") } + let(:article) { create(:article, video_code: "DUMMY_VID_CODE") } + describe "POST /video_states" do it "updates video state" do - post "/video_states?key=#{@user.secret}", params: { - Message: "{\"input\":{\"key\":\"DUMMY_VID_CODE\"}}"}.to_json + input = JSON.unparse(input: { key: article.video_code }) + post "/video_states?key=#{authorized_user.secret}", + params: { Message: input }.to_json expect(Article.last.video_state).to eq("COMPLETED") end + it "rejects non-authorized users" do - @user.remove_role(:super_admin) - post "/video_states?key=#{@user.secret}", params: {input: {key: Article.last.video_code}} + post "/video_states?key=#{regular_user.secret}", + params: { input: { key: article.video_code } } expect(response).to have_http_status(422) end end