Add more test coverage (#1829)

* Create github repos request spec

* Create article_mutes_spec.rb

* Create moderation_service_spec.rb

* Create push notification subscriptions spec

* Add PodcastEpisodeDecorator specs

* Remove randomness in test

* Create specs for ArticleWithVideoCreationService

* Update spec/decorators/podcast_episode_decorator_spec.rb

Co-Authored-By: maestromac <krairit.siri@gmail.com>
This commit is contained in:
Mac Siri 2019-02-22 12:16:57 -05:00 committed by Ben Halpern
parent 8c0966da57
commit 2e647cedb4
8 changed files with 134 additions and 5 deletions

View file

@ -18,7 +18,7 @@ class AsyncInfoController < ApplicationController
end
@user = current_user.decorate
# Updates article analytics periodically:
ArticleAnalyticsFetcher.new.delay.update_analytics(@user.id) if rand(20) == 1
occasionally_update_analytics
respond_to do |format|
format.json do
render json: {
@ -62,4 +62,12 @@ class AsyncInfoController < ApplicationController
#{current_user&.articles_count}__
#{cookies[:remember_user_token]}"
end
private
def occasionally_update_analytics
if Rails.env.production? && rand(25) == 1
ArticleAnalyticsFetcher.new.delay.update_analytics(@user.id)
end
end
end

View file

@ -1,13 +1,19 @@
class PushNotificationSubscriptionsController < ApplicationController
def create
@subscription = PushNotificationSubscription.where(endpoint: params[:subscription][:endpoint]).
@subscription = PushNotificationSubscription.where(endpoint: pns_params[:endpoint]).
first_or_create(
auth_key: params[:subscription][:keys][:auth],
p256dh_key: params[:subscription][:keys][:p256dh],
endpoint: params[:subscription][:endpoint],
auth_key: pns_params[:keys][:auth],
p256dh_key: pns_params[:keys][:p256dh],
endpoint: pns_params[:endpoint],
user_id: current_user.id,
notification_type: "browser",
)
render json: { status: "success", endpoint: @subscription.endpoint }, status: 201
end
private
def pns_params
params.require(:subscription).permit({ keys: %i[auth p256dh] }, :endpoint)
end
end

View file

@ -0,0 +1,10 @@
require "rails_helper"
RSpec.describe PodcastEpisodeDecorator, type: :decorator do
describe "#comments_to_show_count" do
it "returns 25 if does not have a discuss tag" do
pe = build_stubbed(:podcast_episode).decorate
expect(pe.comments_to_show_count).to eq(25)
end
end
end

View file

@ -0,0 +1,32 @@
require "rails_helper"
RSpec.describe "Api::V0::GithubRepos", type: :request do
let(:user) { create(:user) }
let(:repo) { build(:github_repo, user: user) }
let(:my_ocktokit_client) { instance_double(Octokit::Client) }
let(:stubbed_github_repos) do
[OpenStruct.new(repo.attributes.merge(id: repo.github_id_code, html_url: Faker::Internet.url))]
end
before do
allow(Octokit::Client).to receive(:new).and_return(my_ocktokit_client)
allow(my_ocktokit_client).to receive(:repositories) { stubbed_github_repos }
sign_in user
end
describe "GET /api/v0/github_repos" do
it "returns 200 on success" do
get "/api/github_repos"
expect(response).to have_http_status(200)
end
end
describe "POST /api/v0/github_repos/update_or_create" do
it "returns 200 and json response on success" do
param = stubbed_github_repos.first.to_h.to_json
post "/api/github_repos/update_or_create", params: { github_repo: param }
expect(response).to have_http_status(200)
expect(response.content_type).to eq("application/json")
end
end
end

View file

@ -0,0 +1,16 @@
require "rails_helper"
RSpec.describe "ArticleMutes", type: :request do
let(:user) { create(:user) }
before { sign_in user }
describe "GET /article_mutes" do
it "returns 302 upon success" do
article = create(:article, user: user)
patch "/article_mutes/#{article.id}",
params: { article: { receive_notifications: false } }
expect(response).to have_http_status(302)
end
end
end

View file

@ -0,0 +1,20 @@
require "rails_helper"
RSpec.describe "PushNotificationSubscriptions", type: :request do
let(:user) { create(:user) }
before { sign_in user }
describe "POST /push_notification_subscriptions" do
it "works" do
post "/push_notification_subscriptions", params: {
subscription: {
keys: { auth: "random", p256dh: "random" },
endpoint: "random"
}
}
expect(response).to have_http_status(201)
expect(JSON.parse(response.body)["endpoint"]).to eq("random")
end
end
end

View file

@ -0,0 +1,20 @@
require "rails_helper"
RSpec.describe ArticleWithVideoCreationService, type: :service do
let(:link) { "https://s3.amazonaws.com/dev-to-input-v0/video-upload__2d7dc29e39a40c7059572bca75bb646b" }
before do
stub_request(:get, /cloudfront.net/).to_return(status: 200, body: "", headers: {})
end
describe "#create!" do
it "works" do
Timecop.travel(3.weeks.ago)
user = create(:user)
Timecop.return
test = build_stubbed(:article, user: user, video: link).attributes.symbolize_keys
article = described_class.new(test, user).create!
expect(article.video_state).to eq("PROGRESSING")
end
end
end

View file

@ -0,0 +1,17 @@
require "rails_helper"
RSpec.describe ModerationService do
let(:mod) { create(:user, :trusted) }
let(:article) { create(:user) }
describe "#send_moderation_notification" do
it "sends Notification to a moderator" do
mod
allow(Notification).to receive(:create)
run_background_jobs_immediately do
described_class.new.send_moderation_notification(article)
end
expect(Notification).to have_received(:create).exactly(:once)
end
end
end