Skip to content

Commit

Permalink
Merge pull request rpush#528 from splitwise/jess/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
benlangfeld authored Sep 24, 2020
2 parents 2af2d52 + 3eeac89 commit 88acc4c
Show file tree
Hide file tree
Showing 54 changed files with 1,159 additions and 1,240 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.6.3
2.6.5
1 change: 0 additions & 1 deletion lib/rpush/client/redis/pushy/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Notification < Rpush::Client::Redis::Notification

def time_to_live=(value)
self.expiry = value
super
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/simplecov_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def start_simple_cov(name)
end
end

formatter SimpleCov::Formatter::MultiFormatter[*formatters]
formatter SimpleCov::Formatter::MultiFormatter.new(formatters)
end
end
end
56 changes: 2 additions & 54 deletions spec/unit/client/active_record/adm/app_spec.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,6 @@
require 'unit_spec_helper'

describe Rpush::Client::ActiveRecord::Adm::App do
subject { Rpush::Client::ActiveRecord::Adm::App.new(name: 'test', environment: 'development', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }
let(:existing_app) { Rpush::Client::ActiveRecord::Adm::App.create!(name: 'existing', environment: 'development', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }

it 'should be valid if properly instantiated' do
expect(subject).to be_valid
end

it 'should be invalid if name' do
subject.name = nil
expect(subject).not_to be_valid
expect(subject.errors[:name]).to eq ["can't be blank"]
end

it 'should be invalid if name is not unique within scope' do
subject.name = existing_app.name
expect(subject).not_to be_valid
expect(subject.errors[:name]).to eq ["has already been taken"]
end

it 'should be invalid if missing client_id' do
subject.client_id = nil
expect(subject).not_to be_valid
expect(subject.errors[:client_id]).to eq ["can't be blank"]
end

it 'should be invalid if missing client_secret' do
subject.client_secret = nil
expect(subject).not_to be_valid
expect(subject.errors[:client_secret]).to eq ["can't be blank"]
end

describe '#access_token_expired?' do
before(:each) do
Timecop.freeze(Time.now)
end

after do
Timecop.return
end

it 'should return true if access_token_expiration is nil' do
expect(subject.access_token_expired?).to eq(true)
end

it 'should return true if expired' do
subject.access_token_expiration = Time.now - 5.minutes
expect(subject.access_token_expired?).to eq(true)
end

it 'should return false if not expired' do
subject.access_token_expiration = Time.now + 5.minutes
expect(subject.access_token_expired?).to eq(false)
end
end
it_behaves_like 'Rpush::Client::Adm::App'
it_behaves_like 'Rpush::Client::ActiveRecord::App'
end if active_record?
41 changes: 2 additions & 39 deletions spec/unit/client/active_record/adm/notification_spec.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
require 'unit_spec_helper'
require 'unit/notification_shared.rb'

describe Rpush::Client::ActiveRecord::Adm::Notification do
it_should_behave_like 'an Notification subclass'

let(:app) { Rpush::Client::ActiveRecord::Adm::App.create!(name: 'test', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }
let(:notification_class) { Rpush::Client::ActiveRecord::Adm::Notification }
let(:notification) { notification_class.new }

it "has a 'data' payload limit of 6144 bytes" do
notification.data = { key: "a" * 6144 }
expect(notification.valid?).to eq(false)
expect(notification.errors[:base]).to eq ["Notification payload data cannot be larger than 6144 bytes."]
end

it 'limits the number of registration ids to 100' do
notification.registration_ids = ['a'] * (100 + 1)
expect(notification.valid?).to eq(false)
expect(notification.errors[:base]).to eq ["Number of registration_ids cannot be larger than 100."]
end

it 'validates data can be blank if collapse_key is set' do
notification.app = app
notification.registration_ids = 'a'
notification.collapse_key = 'test'
notification.data = nil
expect(notification.valid?).to eq(true)
expect(notification.errors[:data]).to be_empty
end

it 'validates data is present if collapse_key is not set' do
notification.collapse_key = nil
notification.data = nil
expect(notification.valid?).to eq(false)
expect(notification.errors[:data]).to eq ['must be set unless collapse_key is specified']
end

it 'includes expiresAfter in the payload' do
notification.expiry = 100
expect(notification.as_json['expiresAfter']).to eq 100
end
it_behaves_like 'Rpush::Client::Adm::Notification'
it_behaves_like 'Rpush::Client::ActiveRecord::Notification'
end if active_record?
29 changes: 3 additions & 26 deletions spec/unit/client/active_record/apns/app_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
require 'unit_spec_helper'

describe Rpush::Client::ActiveRecord::App do
it 'does not validate an app with an invalid certificate' do
app = Rpush::Client::ActiveRecord::Apns::App.new(name: 'test', environment: 'development', certificate: 'foo')
app.valid?
expect(app.errors[:certificate]).to eq ['value must contain a certificate and a private key.']
end

it 'validates a certificate without a password' do
app = Rpush::Client::ActiveRecord::Apns::App.new name: 'test', environment: 'development', certificate: TEST_CERT
app.valid?
expect(app.errors[:certificate]).to eq []
end

it 'validates a certificate with a password' do
app = Rpush::Client::ActiveRecord::Apns::App.new name: 'test', environment: 'development',
certificate: TEST_CERT_WITH_PASSWORD, password: 'fubar'
app.valid?
expect(app.errors[:certificate]).to eq []
end

it 'validates a certificate with an incorrect password' do
app = Rpush::Client::ActiveRecord::Apns::App.new name: 'test', environment: 'development',
certificate: TEST_CERT_WITH_PASSWORD, password: 'incorrect'
app.valid?
expect(app.errors[:certificate]).to eq ['value must contain a certificate and a private key.']
end
describe Rpush::Client::ActiveRecord::Apns::App do
it_behaves_like 'Rpush::Client::Apns::App'
it_behaves_like 'Rpush::Client::ActiveRecord::App'
end if active_record?
6 changes: 1 addition & 5 deletions spec/unit/client/active_record/apns/feedback_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
require 'unit_spec_helper'

describe Rpush::Client::ActiveRecord::Apns::Feedback do
it 'validates the format of the device_token' do
notification = Rpush::Client::ActiveRecord::Apns::Feedback.new(device_token: "{$%^&*()}")
expect(notification.valid?).to be_falsey
expect(notification.errors[:device_token]).to include('is invalid')
end
it_behaves_like 'Rpush::Client::Apns::Feedback'
end if active_record?
Loading

0 comments on commit 88acc4c

Please sign in to comment.