Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #87 Add some SlackNotificationService tests and gral Rubocop warnings cleanup #89

Merged
merged 4 commits into from
Jan 24, 2023

Conversation

megatux
Copy link
Contributor

@megatux megatux commented Jan 11, 2023

Summary

Trying to tackle #87 by proving that with a PR from a blacklisted user it does not sends the message.

Other Information

none

@@ -4,7 +4,7 @@ class PullRequest
:url, :body, :language, :repo_name

def initialize(params)
@info = params.dig(:pull_request)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to avoid nil errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Params never gonna be nil so why this can raise an error? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know anything about the Slack connection/gem/client/etc usages (I still don't).
I just started to test the class in isolation and noticed that it was raising a lot errors because it assumes a complex parameter tree structure. I usually try to validate initialization/params just a bit, to avoid ugly bugs later in the code (most of the time, undefined xxxx for nil errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes a complex input structure is a smell that could be improve/cleaned, sometimes with separated params or a Struct/PORO

@action = params[:github_webhook][:action]
@extra_params = params
@action = params.dig(:github_webhook, :action)
raise(ArgumentError, 'No action param set') unless @action
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to avoid wrong initializations

@@ -56,7 +58,7 @@ def filter_on_hold_labeled_action

def channel(params)
lang = LANGUAGES[params.dig(:repository, :language)&.to_sym]
repository_info = params.dig(:repository)
repository_info = params[:repository]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubocop fix

@@ -68,7 +70,7 @@ def channel(params)

def search_channel(channel)
Slack::Web::Client.new.conversations_info(channel: channel)
rescue Exception => e
rescue StandardError => e
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubocop fix

def js_channels(pr, lang)
return unless pr[:name]
def js_channels(pull_request, _lang)
return unless pull_request[:name]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubocop fixes

end

context 'with a PR from a blacklisted user' do
let!(:user) { User.create(github_name: 'pepe', blacklisted: true) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that with an existing dependabot user should work, too.

@@ -0,0 +1,48 @@
require 'rails_helper'

describe SlackNotificationService do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe SlackNotificationService do
describe SlackNotificationService do
describe '#initialize' do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. My idea was to explore the class code by creating scenarios. First, started with initialization scenarios, quickly finding issues with nil errors when Hash structure is not the expected one. Added a raise on #initialize and 🔁 but only one scenario did into the PR. I could add more scenarios or change it to #initialize as you proposed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I like this way

spec/services/slack_notification_service_spec.rb Outdated Show resolved Hide resolved
spec/services/slack_notification_service_spec.rb Outdated Show resolved Hide resolved
@megatux megatux requested a review from adlamas January 13, 2023 21:14
@megatux
Copy link
Contributor Author

megatux commented Jan 13, 2023

Added another commit with some better params setup requiring extra web stubbing.
This service class screams for a refactor extracting the channel logic into a separate class for an appropriate testing.

Copy link
Contributor

@adlamas adlamas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite comments, it's ok to me

@@ -0,0 +1,48 @@
require 'rails_helper'

describe SlackNotificationService do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I like this way

stub_request(:post, 'https://slack.com/api/conversations.list')
.to_return(status: 200, body: '', headers: {})

# stub_request(:post, 'https://slack.com/api/conversations.info')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

during test construction some execution paths required the stubing of this endpoint, too. Then I simplified the scenarios and was no longer required. I kept it just in case, but maybe it's better to cleanup the code as much as possible. I can remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it's better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

end

describe '#send_notification' do
subject { described_class.new(params).send_notification }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this. Isn't it better if we only create the object in the "subject", and after call the method? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what style is better. The subject under test in this scenario is the method

@jeroig jeroig self-requested a review January 17, 2023 13:24
@megatux megatux merged commit d09633f into master Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants