Skip to content

Commit

Permalink
refactor: refactor utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanAlvarez21 committed Jan 30, 2025
1 parent 3a66a84 commit a452c5a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 115 deletions.
20 changes: 10 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
69 changes: 49 additions & 20 deletions spec/bas/utils/discord/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/bas/utils/discord/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -82,4 +82,4 @@
)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/bas/utils/exceptions/invalid_process_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
expect(described_class).to be < StandardError
end
end
end
end
47 changes: 31 additions & 16 deletions spec/bas/utils/google/send_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
8 changes: 5 additions & 3 deletions spec/bas/utils/notion/delete_page_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,4 +20,4 @@
expect(Utils::Notion::Request).to have_received(:execute).with(hash_including(method: "delete")).twice
end
end
end
end
77 changes: 14 additions & 63 deletions spec/bas/utils/openai/run_assistan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a452c5a

Please sign in to comment.