From 87f01506cb0f0ba9ee2d7153157b0a07d628a559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 16 Dec 2017 01:44:36 +0100 Subject: [PATCH] Add CheckGCPProjectBillingService --- .../check_gcp_project_billing_service.rb | 8 +++++ .../check_gcp_project_billing_service_spec.rb | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 app/services/check_gcp_project_billing_service.rb create mode 100644 spec/services/check_gcp_project_billing_service_spec.rb diff --git a/app/services/check_gcp_project_billing_service.rb b/app/services/check_gcp_project_billing_service.rb new file mode 100644 index 00000000000..45192a167eb --- /dev/null +++ b/app/services/check_gcp_project_billing_service.rb @@ -0,0 +1,8 @@ +class CheckGCPProjectBillingService + def execute(token) + client = GoogleApi::CloudPlatform::Client.new(token, nil) + client.projects_list.any? do |project| + client.projects_get_billing_info(project.name).billingEnabled + end + end +end diff --git a/spec/services/check_gcp_project_billing_service_spec.rb b/spec/services/check_gcp_project_billing_service_spec.rb new file mode 100644 index 00000000000..b7c42fcace1 --- /dev/null +++ b/spec/services/check_gcp_project_billing_service_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe CheckGCPProjectBillingService do + let(:service) { described_class.new } + + describe '#execute' do + before do + expect_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive(:projects_list).and_return([double(name: 'project_name')]) + + expect_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive_message_chain(:projects_get_billing_info, :billingEnabled) + .and_return(project_billing_enabled) + end + + subject { service.execute('bogustoken') } + + context 'google account has a billing enabled gcp project' do + let(:project_billing_enabled) { true } + + it { is_expected.to eq(true) } + end + + context 'google account does not have a billing enabled gcp project' do + let(:project_billing_enabled) { false } + + it { is_expected.to eq(false) } + end + end +end