Skip to content

Commit

Permalink
fix(tests/NoCredentialsError): use aws_credentials fixture (#3131)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi committed Dec 11, 2023
1 parent ad94ada commit 7883e28
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 16 deletions.
6 changes: 0 additions & 6 deletions api/.env-ci
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@ DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres
ANALYTICS_DATABASE_URL=postgres://postgres:postgres@localhost:5432/analytics
PYTEST_ADDOPTS=--cov . --cov-report xml -n auto --dist worksteal

# used by moto # ref https://github.com/getmoto/moto/issues/5941
AWS_ACCESS_KEY_ID=testing
AWS_SECRET_ACCESS_KEY=testing
AWS_SECURITY_TOKEN=testing
AWS_SESSION_TOKEN=testing
AWS_DEFAULT_REGION=eu-east-2
5 changes: 2 additions & 3 deletions api/sse/sse_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from sse import tasks
from sse.dataclasses import SSEAccessLogs

s3 = boto3.resource("s3")


def _sse_enabled(get_project_from_first_arg=lambda obj: obj.project):
"""
Expand Down Expand Up @@ -55,7 +53,8 @@ def send_environment_update_message_for_environment(environment):

def stream_access_logs() -> Generator[SSEAccessLogs, None, None]:
gpg = gnupg.GPG()
bucket = s3.Bucket(settings.AWS_SSE_LOGS_BUCKET_NAME)
bucket = boto3.resource("s3").Bucket(settings.AWS_SSE_LOGS_BUCKET_NAME)

for log_file in bucket.objects.all():
encrypted_body = log_file.get()["Body"].read()
decrypted_body = gpg.decrypt(encrypted_body)
Expand Down
8 changes: 1 addition & 7 deletions api/tests/unit/sse/test_sse_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import pytest
from django.conf import settings
from moto import mock_s3
from moto.core import patch_resource
from pytest_lazyfixture import lazy_fixture
from pytest_mock import MockerFixture

from sse.dataclasses import SSEAccessLogs
from sse.sse_service import (
s3,
send_environment_update_message_for_environment,
send_environment_update_message_for_project,
stream_access_logs,
Expand Down Expand Up @@ -107,7 +105,7 @@ def test_send_environment_update_message_for_environment_schedules_task_correctl


@mock_s3
def test_stream_access_logs(mocker: MockerFixture):
def test_stream_access_logs(mocker: MockerFixture, aws_credentials: None) -> None:
# Given - Some test data
first_log = SSEAccessLogs("2023-11-27T06:42:47+0000", "key_one")
second_log = SSEAccessLogs("2023-11-27T06:42:47+0000", "key_two")
Expand All @@ -123,10 +121,6 @@ def test_stream_access_logs(mocker: MockerFixture):
f"{third_log.generated_at},{third_log.api_key}".encode()
)

# patch the s3 resource because it was created before the mock_s3 decorator was applied
# ref: https://docs.getmoto.org/en/latest/docs/getting_started.html#patching-the-client-or-resource
patch_resource(s3)

# Next, let's create a bucket
bucket_name = settings.AWS_SSE_LOGS_BUCKET_NAME
s3_client = boto3.client("s3", region_name="eu-west-2")
Expand Down

3 comments on commit 7883e28

@vercel
Copy link

@vercel vercel bot commented on 7883e28 Dec 11, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 7883e28 Dec 11, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 7883e28 Dec 11, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io

Please sign in to comment.