|
1 | 1 | require "spec_helper" |
| 2 | +require 'tempfile' |
2 | 3 |
|
3 | 4 | describe FCM do |
4 | 5 | let(:project_name) { 'test-project' } |
|
13 | 14 | } |
14 | 15 | end |
15 | 16 |
|
| 17 | + let(:client_email) do |
| 18 | + '83315528762cf7e0-7bbcc3aad87e0083391bc7f234d487' \ |
| 19 | + 'c8@developer.gserviceaccount.com' |
| 20 | + end |
| 21 | + |
| 22 | + let(:client_x509_cert_url) do |
| 23 | + 'https://www.googleapis.com/robot/v1/metadata/x509/' \ |
| 24 | + 'fd6b61037dd2bb8585527679" + "-7bbcc3aad87e0083391b' \ |
| 25 | + 'c7f234d487c8%40developer.gserviceaccount.com' |
| 26 | + end |
| 27 | + |
| 28 | + let(:large_file_name) do |
| 29 | + Array.new(1021) { 'a' }.join('') + '.txt' |
| 30 | + end |
| 31 | + |
| 32 | + let(:creds_error) do |
| 33 | + FCM::InvalidCredentialError |
| 34 | + end |
| 35 | + |
| 36 | + let(:json_credentials) do |
| 37 | + { |
| 38 | + "type": 'service_account', |
| 39 | + "project_id": 'example', |
| 40 | + "private_key_id": 'c09c4593eee53707ca9f4208fbd6fe72b29fc7ab', |
| 41 | + "private_key": OpenSSL::PKey::RSA.new(2048).to_s, |
| 42 | + "client_email": client_email, |
| 43 | + "client_id": 'acedc3c0a63b3562376386f0.apps.googleusercontent.com', |
| 44 | + "auth_uri": 'https://accounts.google.com/o/oauth2/auth', |
| 45 | + "token_uri": 'https://oauth2.googleapis.com/token', |
| 46 | + "auth_provider_x509_cert_url": 'https://www.googleapis.com/oauth2/v1/certs', |
| 47 | + "client_x509_cert_url": client_x509_cert_url, |
| 48 | + "universe_domain": 'googleapis.com' |
| 49 | + }.to_json |
| 50 | + end |
| 51 | + |
16 | 52 | before do |
17 | 53 | allow(client).to receive(:json_key) |
18 | 54 |
|
19 | 55 | # Mock the Google::Auth::ServiceAccountCredentials |
20 | | - allow(Google::Auth::ServiceAccountCredentials).to receive(:make_creds). |
21 | | - and_return(double(fetch_access_token!: { 'access_token' => mock_token })) |
| 56 | + allow(Google::Auth::ServiceAccountCredentials).to receive(:make_creds) |
| 57 | + .and_return(double(fetch_access_token!: { 'access_token' => mock_token })) |
22 | 58 | end |
23 | 59 |
|
24 | | - it "should initialize" do |
| 60 | + it 'should initialize' do |
25 | 61 | expect { client }.not_to raise_error |
26 | 62 | end |
27 | 63 |
|
28 | 64 | describe "credentials path" do |
29 | | - it "can be a path to a file" do |
| 65 | + it 'can be a path to a file' do |
30 | 66 | fcm = FCM.new("README.md") |
31 | 67 | expect(fcm.__send__(:json_key).class).to eq(File) |
32 | 68 | end |
33 | 69 |
|
34 | | - it "can be an IO object" do |
35 | | - fcm = FCM.new(StringIO.new("hey")) |
| 70 | + it 'raises an error when passed a large path' do |
| 71 | + expect do |
| 72 | + FCM.new(large_file_name).__send__(:json_key) |
| 73 | + end.to raise_error(creds_error) |
| 74 | + end |
| 75 | + |
| 76 | + it 'can be an IO object' do |
| 77 | + fcm = FCM.new(StringIO.new('hey')) |
36 | 78 | expect(fcm.__send__(:json_key).class).to eq(StringIO) |
| 79 | + |
| 80 | + temp_file = Tempfile.new('hello_world.json') |
| 81 | + temp_file.write(json_credentials) |
| 82 | + fcm_with_temp_file = FCM.new(temp_file) |
| 83 | + |
| 84 | + expect do |
| 85 | + fcm_with_temp_file |
| 86 | + end.not_to raise_error |
| 87 | + temp_file.close |
| 88 | + temp_file.unlink |
| 89 | + end |
| 90 | + |
| 91 | + it 'raises an error when passed a non IO-like object' do |
| 92 | + expect do |
| 93 | + FCM.new(nil, '', {}).__send__(:json_key) |
| 94 | + end.to raise_error(creds_error, 'credentials must be' \ |
| 95 | + ' an IO-like object or path. You passed nil.') |
| 96 | + |
| 97 | + expect do |
| 98 | + FCM.new(json_credentials, '', {}).__send__(:json_key) |
| 99 | + end.to raise_error(creds_error, 'credentials must be' \ |
| 100 | + ' an IO-like object or path. You passed a String.') |
| 101 | + |
| 102 | + expect do |
| 103 | + FCM.new({}, '', {}).__send__(:json_key) |
| 104 | + end.to raise_error(creds_error, 'credentials must be' \ |
| 105 | + ' an IO-like object or path. You passed a Hash.') |
| 106 | + end |
| 107 | + |
| 108 | + it 'raises an error when passed a non-existent credentials file path' do |
| 109 | + fcm = FCM.new('spec/fake_credentials.json', '', {}) |
| 110 | + expect { fcm.__send__(:json_key) }.to raise_error(creds_error) |
| 111 | + end |
| 112 | + |
| 113 | + it 'raises an error when passed a string of a file that does not exist' do |
| 114 | + fcm = FCM.new('example.txt', '', {}) |
| 115 | + expect { fcm.__send__(:json_key) }.to raise_error(creds_error) |
37 | 116 | end |
38 | 117 | end |
39 | 118 |
|
|
0 commit comments