From a452c5a4c70cf45e9bfc2a9bb10eddc2988d926f Mon Sep 17 00:00:00 2001 From: Alan Daniel Alvarez Castro Date: Thu, 30 Jan 2025 12:59:58 -0600 Subject: [PATCH] refactor: refactor utils tests --- Gemfile | 20 ++--- spec/bas/utils/discord/integration_spec.rb | 69 ++++++++++++----- spec/bas/utils/discord/request_spec.rb | 4 +- .../invalid_process_response_spec.rb | 2 +- spec/bas/utils/google/send_email_spec.rb | 47 +++++++---- .../utils/notion/delete_page_blocks_spec.rb | 8 +- spec/bas/utils/openai/run_assistan_spec.rb | 77 ++++--------------- 7 files changed, 112 insertions(+), 115 deletions(-) diff --git a/Gemfile b/Gemfile index 2a8c3b1..2f37e43 100644 --- a/Gemfile +++ b/Gemfile @@ -21,13 +21,13 @@ gem "pg", "~> 1.5", ">= 1.5.4" group :test do gem "faraday-retry" - gem 'octokit', '~> 8.1.0' - gem 'jwt', "~> 2.8.1" - gem 'openssl', '~> 3.2' - gem 'md_to_notion', '~> 0.1.4' - gem 'google-api-client', '~> 0.53' - gem 'googleauth', '~> 1.11' - gem 'net-imap', '~> 0.4.10' - gem 'net-smtp', '~> 0.4.0.1' - gem 'gmail_xoauth', '~> 0.4.1' -end \ No newline at end of file + gem "gmail_xoauth", "~> 0.4.1" + gem "google-api-client", "~> 0.53" + gem "googleauth", "~> 1.11" + gem "jwt", "~> 2.8.1" + gem "md_to_notion", "~> 0.1.4" + gem "net-imap", "~> 0.4.10" + gem "net-smtp", "~> 0.4.0.1" + gem "octokit", "~> 8.1.0" + gem "openssl", "~> 3.2" +end diff --git a/spec/bas/utils/discord/integration_spec.rb b/spec/bas/utils/discord/integration_spec.rb index 213f3c9..63908c1 100644 --- a/spec/bas/utils/discord/integration_spec.rb +++ b/spec/bas/utils/discord/integration_spec.rb @@ -3,31 +3,60 @@ require "bas/utils/discord/integration" RSpec.describe Utils::Discord::Integration do - before do - @params = { - webhook: "webhook", - name: "discordBotName", - notification: "notification message" - } - end + let(:params) { { webhook: "https://discord.com/api/webhooks/12345", name: "discordBotName", notification: "notification message" } } + let(:response) { double("http_response", code: 200, body: "success") } + + before { allow(HTTParty).to receive(:post).and_return(response) } + + describe "#execute" do + context "when the request is successful" do + it "sends a request to Discord webhook with the correct parameters" do + described_class.execute(params) + + expect(HTTParty).to have_received(:post).with( + params[:webhook], + body: described_class.body(params), + headers: described_class.headers + ) + end + + it "returns the response from the API" do + result = described_class.execute(params) + expect(result).to eq(response) + end + end + + context "when the webhook URL is invalid" do + let(:params) { { webhook: "invalid_url", name: "discordBotName", notification: "notification message" } } + + it "raises an error" do + allow(HTTParty).to receive(:post).and_raise(URI::InvalidURIError) + expect { described_class.execute(params) }.to raise_error(URI::InvalidURIError) + end + end - describe ".execute" do - let(:response) { double("http_response") } - before { allow(HTTParty).to receive(:post).and_return(response) } + context "when the request fails" do + let(:error_response) { double("http_response", code: 500, body: "error") } - it { expect(described_class.execute(@params)).to_not be_nil } + it "returns the error response" do + allow(HTTParty).to receive(:post).and_return(error_response) + result = described_class.execute(params) + expect(result).to eq(error_response) + end + end end - describe ".body" do - it { - expect(described_class.body(@params)).to eq({ - username: @params[:name], - content: @params[:notification] - }.to_json) - } + describe "#body" do + it "returns the correct body format" do + expect(described_class.body(params)).to eq( + { username: params[:name], content: params[:notification] }.to_json + ) + end end - describe ".headers" do - it { expect(described_class.headers).to eq({ "Content-Type" => "application/json" }) } + describe "#headers" do + it "returns the correct headers" do + expect(described_class.headers).to eq({ "Content-Type" => "application/json" }) + end end end diff --git a/spec/bas/utils/discord/request_spec.rb b/spec/bas/utils/discord/request_spec.rb index 3ee920c..8d367b8 100644 --- a/spec/bas/utils/discord/request_spec.rb +++ b/spec/bas/utils/discord/request_spec.rb @@ -48,7 +48,7 @@ it "sends a request to Discord API with the correct parameters" do combined_paragraphs = "Paragraph 1\n\nParagraph 2" described_class.write_media_text(params, combined_paragraphs) - + expect(HTTParty).to have_received(:post).with( URI.parse("https://discord.com/api/v10/channels/12345/messages"), # Usar URI.parse body: { content: combined_paragraphs }.to_json, @@ -82,4 +82,4 @@ ) end end -end \ No newline at end of file +end diff --git a/spec/bas/utils/exceptions/invalid_process_response_spec.rb b/spec/bas/utils/exceptions/invalid_process_response_spec.rb index 0eea42e..ed37e5e 100644 --- a/spec/bas/utils/exceptions/invalid_process_response_spec.rb +++ b/spec/bas/utils/exceptions/invalid_process_response_spec.rb @@ -25,4 +25,4 @@ expect(described_class).to be < StandardError end end -end \ No newline at end of file +end diff --git a/spec/bas/utils/google/send_email_spec.rb b/spec/bas/utils/google/send_email_spec.rb index 314f3ec..96e6eb4 100644 --- a/spec/bas/utils/google/send_email_spec.rb +++ b/spec/bas/utils/google/send_email_spec.rb @@ -3,8 +3,8 @@ require "bas/utils/google/send_email" RSpec.describe Utils::GoogleService::SendEmail do - before do - @params = { + let(:params) do + { refresh_token: "refresh_token", client_id: "client_id", client_secret: "client_secret", @@ -13,29 +13,44 @@ subject: "email subject", message: "email message" } + end - @service = described_class.new(@params) + let(:refresh_object) do + instance_double("Google::Auth::UserRefreshCredentials", fetch_access_token!: nil, access_token: "ABCD1234") + end + let(:gmail_service) do + instance_double("Google::Apis::GmailV1::GmailService", send_user_message: nil, authorization: nil) end - describe ".execute" do - let(:refresh_object) { double("refresh", fetch_access_token!: nil, access_token: "ABCD1234") } - let(:gmail_service) { double("service", send_user_message: nil, authorization: nil) } + before do + stub_const("Google::Apis::GmailV1::GmailService", Class.new) + stub_const("Google::Auth::UserRefreshCredentials", Class.new) - before do - allow(Google::Auth::UserRefreshCredentials).to receive(:new).and_return(refresh_object) - allow(Google::Apis::GmailV1::GmailService).to receive(:new).and_return(gmail_service) - end + allow(Google::Auth::UserRefreshCredentials).to receive(:new).and_return(refresh_object) + allow(Google::Apis::GmailV1::GmailService).to receive(:new).and_return(gmail_service) + end + + describe ".execute" do + subject(:service) { described_class.new(params) } - it "should return an error message when an error is thrown" do - allow(gmail_service).to receive(:authorization=).and_raise(StandardError) + context "when an error occurs" do + before do + allow(gmail_service).to receive(:authorization=).and_raise(StandardError, "Mocked error") + end - expect(@service.execute).to eq({ error: "StandardError" }) + it "returns an error message" do + expect(service.execute).to eq({ error: "Mocked error" }) + end end - it "should send the email and return an empty response" do - allow(gmail_service).to receive(:authorization=).and_return(nil) + context "when email is sent successfully" do + before do + allow(gmail_service).to receive(:authorization=).and_return(nil) + end - expect(@service.execute).to eq({ send_email: nil }) + it "sends the email and returns an empty response" do + expect(service.execute).to eq({ send_email: nil }) + end end end end diff --git a/spec/bas/utils/notion/delete_page_blocks_spec.rb b/spec/bas/utils/notion/delete_page_blocks_spec.rb index e9bb5c8..148856d 100644 --- a/spec/bas/utils/notion/delete_page_blocks_spec.rb +++ b/spec/bas/utils/notion/delete_page_blocks_spec.rb @@ -4,11 +4,13 @@ RSpec.describe Utils::Notion::DeletePageBlocks do let(:params) { { page_id: "page_123", secret: "notion_secret" } } - let(:mock_response) { double("http_response", parsed_response: { "results" => [{ "id" => "block_1" }, { "id" => "block_2" }] }) } + let(:mock_response) do + double("http_response", parsed_response: { "results" => [{ "id" => "block_1" }, { "id" => "block_2" }] }) + end before do allow(Utils::Notion::Request).to receive(:execute).with(hash_including(endpoint: "blocks/page_123/children")) - .and_return(mock_response) + .and_return(mock_response) allow(Utils::Notion::Request).to receive(:execute).with(hash_including(method: "delete")) end @@ -18,4 +20,4 @@ expect(Utils::Notion::Request).to have_received(:execute).with(hash_including(method: "delete")).twice end end -end \ No newline at end of file +end diff --git a/spec/bas/utils/openai/run_assistan_spec.rb b/spec/bas/utils/openai/run_assistan_spec.rb index ac2f19c..a12b73e 100644 --- a/spec/bas/utils/openai/run_assistan_spec.rb +++ b/spec/bas/utils/openai/run_assistan_spec.rb @@ -3,77 +3,28 @@ require "bas/utils/openai/run_assistant" RSpec.describe Utils::OpenAI::RunAssitant do - let(:run) { double("run", parsed_response: { "id" => "run_id", "thread_id" => "thread_id" }) } + let(:params) { { assistant_id: "assistant_id", secret: "openai_secret", prompt: "prompt" } } + let(:run_response) { double("run_response", parsed_response: { "id" => "run_id", "thread_id" => "thread_id" }) } + let(:completed_response) { { "status" => "completed", "thread_id" => "thread_id" } } + let(:failed_response) { { "status" => "failed" } } before do - @params = { - assistant_id: "assistant_id", - secret: "openai_secret", - prompt: "prompt" - } - - @body = { - assistant_id: @params[:assistant_id], - thread: { - messages: [ - role: "user", - content: @params[:prompt] - ] - } - }.to_json - - @headers = { - "Authorization" => "Bearer #{@params[:secret]}", - "Content-Type" => "application/json", - "OpenAI-Beta" => "assistants=v2" - } - - allow(HTTParty).to receive(:post).and_return(run) + allow(HTTParty).to receive(:post).and_return(run_response) + allow(HTTParty).to receive(:get).and_return(completed_response) end - describe ".execute" do - it "should fail if the thread and run creation fails" do - allow(run).to receive(:code).and_return(404) - - url = "#{described_class::OPENAI_BASE_URL}/v1/threads/runs" - - expect(HTTParty).to receive(:post).with(url, { body: @body, headers: @headers }) - - response = described_class.execute(@params) - - expect(response.code).to eq(404) + describe "#execute" do + it "fails when thread and run creation fails" do + allow(run_response).to receive(:code).and_return(404) + expect(described_class.execute(params).code).to eq(404) end - it "should fail if the poll run fails" do - allow(run).to receive(:code).and_return(200) - allow(HTTParty).to receive(:get).and_return({ "status" => "in_progress" }, { "status" => "failed" }) - - run_id = run.parsed_response["id"] - thread_id = run.parsed_response["thread_id"] - - url = "#{described_class::OPENAI_BASE_URL}/v1/threads/#{thread_id}/runs/#{run_id}" - - expect(HTTParty).to receive(:get).with(url, { headers: @headers }) - - response = described_class.execute(@params) + it "fails if the poll run fails" do + allow(run_response).to receive(:code).and_return(200) + allow(HTTParty).to receive(:get).and_return({ "status" => "in_progress" }, failed_response) + response = described_class.execute(params) expect(response["status"]).to eq("failed") end - - it "should call the list_message endpoint" do - allow(run).to receive(:code).and_return(200) - allow(HTTParty).to receive(:get).and_return({ "status" => "completed", "thread_id" => "thread_id" }) - - run_id = run.parsed_response["id"] - thread_id = run.parsed_response["thread_id"] - - url_pol = "#{described_class::OPENAI_BASE_URL}/v1/threads/#{thread_id}/runs/#{run_id}" - url_list = "#{described_class::OPENAI_BASE_URL}/v1/threads/#{thread_id}/messages" - - expect(HTTParty).to receive(:get).with(url_pol, { headers: @headers }) - expect(HTTParty).to receive(:get).with(url_list, { headers: @headers }) - - described_class.execute(@params) - end end end