Add minimal tests for ClientSecrets
authorSteven Bazyl <sqrrrl@gmail.com>
Tue, 16 Dec 2014 21:47:26 +0000 (13:47 -0800)
committerSteven Bazyl <sqrrrl@gmail.com>
Tue, 16 Dec 2014 21:47:26 +0000 (13:47 -0800)
lib/google/api_client/client_secrets.rb
spec/fixtures/files/client_secrets.json [new file with mode: 0644]
spec/google/api_client/client_secrets_spec.rb [new file with mode: 0644]

index 792f1b7cf2f507b0a5fef1579f5ee7a00a09d39c..a9cc2413896cd5edff4dccc783f80a7fb77b9d8b 100644 (file)
@@ -88,12 +88,12 @@ module Google
         @client_id = fdata[:client_id] || fdata["client_id"]
         @client_secret = fdata[:client_secret] || fdata["client_secret"]
         @redirect_uris = fdata[:redirect_uris] || fdata["redirect_uris"]
-        @redirect_uris ||= [fdata[:redirect_uri]]
+        @redirect_uris ||= [fdata[:redirect_uri] || fdata["redirect_uri"]].compact
         @javascript_origins = (
           fdata[:javascript_origins] ||
           fdata["javascript_origins"]
         )
-        @javascript_origins ||= [fdata[:javascript_origin]]
+        @javascript_origins ||= [fdata[:javascript_origin] || fdata["javascript_origin"]].compact
         @authorization_uri = fdata[:auth_uri] || fdata["auth_uri"]
         @authorization_uri ||= fdata[:authorization_uri]
         @token_credential_uri = fdata[:token_uri] || fdata["token_uri"]
@@ -120,7 +120,11 @@ module Google
       # @return [String]
       #   JSON
       def to_json
-        return MultiJson.dump({
+        return MultiJson.dump(to_hash)
+      end
+      
+      def to_hash
+        {
           self.flow => ({
             'client_id' => self.client_id,
             'client_secret' => self.client_secret,
@@ -141,7 +145,7 @@ module Google
             end
             accu
           end
-        })
+        }
       end
       
       def to_authorization
diff --git a/spec/fixtures/files/client_secrets.json b/spec/fixtures/files/client_secrets.json
new file mode 100644 (file)
index 0000000..05fa7cb
--- /dev/null
@@ -0,0 +1 @@
+{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"i8YaXdGgiQ4_KrTVNGsB7QP1","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"","client_x509_cert_url":"","client_id":"898243283568.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}
\ No newline at end of file
diff --git a/spec/google/api_client/client_secrets_spec.rb b/spec/google/api_client/client_secrets_spec.rb
new file mode 100644 (file)
index 0000000..ead9bf7
--- /dev/null
@@ -0,0 +1,53 @@
+# encoding:utf-8
+
+# Copyright 2013 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require 'spec_helper'
+
+require 'google/api_client/client_secrets'
+
+FIXTURES_PATH = File.expand_path('../../../fixtures', __FILE__)
+
+RSpec.describe Google::APIClient::ClientSecrets do
+  
+  context 'with JSON file' do
+    let(:file) { File.join(FIXTURES_PATH, 'files', 'client_secrets.json') }
+    subject(:secrets) { Google::APIClient::ClientSecrets.load(file)}
+  
+    it 'should load the correct client ID' do
+      expect(secrets.client_id).to be == '898243283568.apps.googleusercontent.com'
+    end
+
+    it 'should load the correct client secret' do
+      expect(secrets.client_secret).to be == 'i8YaXdGgiQ4_KrTVNGsB7QP1'
+    end
+    
+    context 'serialzed to hash' do
+      subject(:hash) { secrets.to_hash }
+      it 'should contain the flow as the first key' do
+        expect(hash).to have_key "installed"
+      end
+
+      it 'should contain the client ID' do
+        expect(hash["installed"]["client_id"]).to be == '898243283568.apps.googleusercontent.com'
+      end
+
+      it 'should contain the client secret' do
+        expect(hash["installed"]["client_secret"]).to be == 'i8YaXdGgiQ4_KrTVNGsB7QP1'
+      end
+
+    end
+  end
+end
\ No newline at end of file