Skip to content

Add more tests & fix a data deletion bug in AmazonS3InstallationStore#1081

Merged
seratch merged 2 commits intoslackapi:mainfrom
seratch:add-more-tests
Aug 6, 2021
Merged

Add more tests & fix a data deletion bug in AmazonS3InstallationStore#1081
seratch merged 2 commits intoslackapi:mainfrom
seratch:add-more-tests

Conversation

@seratch
Copy link
Copy Markdown
Contributor

@seratch seratch commented Aug 1, 2021

Summary

This pull request adds more unit tests for better code coverage. Particularly, the following patterns are now covered:

  • Error patterns in AuditLogsClient
  • Amazon S3 backend OAuth stores (installation store & state store)

While writing the tests, I found a bug in AmazonS3InstallationStore's installation deletion methods. This pull request fixes the bug as well.

Category (place an x in each of the [ ])

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs-src (Documents, have you run ./docs.sh?)
  • /docs-src-v2 (Documents, have you run ./docs-v2.sh?)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements (place an x in each [ ])

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

@seratch seratch added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented Version: 3x oauth labels Aug 1, 2021
@seratch seratch added this to the 3.9.0 milestone Aug 1, 2021
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 1, 2021

Codecov Report

Merging #1081 (e2ddba5) into main (8f65b94) will increase coverage by 1.79%.
The diff coverage is 63.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1081      +/-   ##
==========================================
+ Coverage   83.86%   85.66%   +1.79%     
==========================================
  Files          99       99              
  Lines        9304     9324      +20     
==========================================
+ Hits         7803     7987     +184     
+ Misses       1501     1337     -164     
Impacted Files Coverage Δ
slack_sdk/errors/__init__.py 100.00% <ø> (ø)
...sdk/oauth/installation_store/amazon_s3/__init__.py 81.76% <63.63%> (+81.76%) ⬆️
slack_sdk/socket_mode/builtin/connection.py 36.13% <0.00%> (+0.42%) ⬆️
slack_sdk/socket_mode/builtin/client.py 79.86% <0.00%> (+0.67%) ⬆️
slack_sdk/socket_mode/client.py 72.56% <0.00%> (+2.65%) ⬆️
slack_sdk/audit_logs/v1/client.py 89.53% <0.00%> (+11.62%) ⬆️
slack_sdk/oauth/state_store/amazon_s3/__init__.py 92.85% <0.00%> (+92.85%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8f65b94...e2ddba5. Read the comment docs.

Copy link
Copy Markdown
Contributor Author

@seratch seratch left a comment

Choose a reason for hiding this comment

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

comments for reviewers

except Exception as e: # skipcq: PYL-W0703
message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}"
self.logger.warning(message)
raise SlackClientConfigurationError(message)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For deletion method failures, we should raise an error than just writing warning-level logs

raise SlackClientConfigurationError(message)

try:
no_user_id_key = key.replace(f"-{user_id}", "")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When saving a new installation, this implementation stores two historical data:
/111.222/E111-T111/installer-1627859348.984687 and /111.222/E111-T111/installer-U111-1627859348.984687 to support both find_installation(enterprise_id, team_id) and find_installation(enterprise_id, team_id, user_id) without performance overhead (= scanning all the keys under the directory). This deletion method used to delete only the first one.

if c.get("Key") not in deleted_keys
]
# If only installer-latest remains, we should delete the one as well
if len(keys) == 1 and keys[0].endswith("installer-latest"):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If only the /111.222/E111-T111/installer-latest remains here, this means the installer-latest is no longer valid.

keys = [
c.get("Key")
for c in objects.get("Contents", [])
if c.get("Key") not in deleted_keys
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As S3 storage is now strongly consistent, we may not need to remove these keys, though. https://aws.amazon.com/blogs/aws/amazon-s3-update-strong-read-after-write-consistency/

import unittest

import boto3
from moto import mock_s3
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We have been using moto in bolt-python project already but this is the first test using it in this project https://github.com/spulec/moto#example-on-unittest-usage

@seratch
Copy link
Copy Markdown
Contributor Author

seratch commented Aug 4, 2021

Let me know by the end of this week if anyone has comments on these changes

@seratch seratch merged commit c2b5c29 into slackapi:main Aug 6, 2021
@seratch seratch deleted the add-more-tests branch August 6, 2021 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented oauth Version: 3x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant