Skip to content

Commit

Permalink
Merge pull request #158 from soehlert/hotfix/USStateField
Browse files Browse the repository at this point in the history
fix(USStateField): Don't save a US State field for non US countries
  • Loading branch information
soehlert authored Oct 25, 2023
2 parents e905a5c + b72a02e commit 0ea01d4
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 38 deletions.
16 changes: 8 additions & 8 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ updates:
directory: '/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'

# Enable version updates for Docker
# We need to specify each Dockerfile in a separate entry because Dependabot doesn't
Expand All @@ -19,7 +19,7 @@ updates:
directory: 'compose/local/django/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'
# Ignore minor version updates (3.10 -> 3.11) but update patch versions
ignore:
- dependency-name: '*'
Expand All @@ -32,21 +32,21 @@ updates:
directory: 'compose/local/node/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'

- package-ecosystem: 'docker'
# Look for a `Dockerfile` in the `compose/production/aws` directory
directory: 'compose/production/aws/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'

- package-ecosystem: 'docker'
# Look for a `Dockerfile` in the `compose/production/django` directory
directory: 'compose/production/django/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'
# Ignore minor version updates (3.10 -> 3.11) but update patch versions
ignore:
- dependency-name: '*'
Expand All @@ -59,14 +59,14 @@ updates:
directory: 'compose/production/postgres/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'

- package-ecosystem: 'docker'
# Look for a `Dockerfile` in the `compose/production/traefik` directory
directory: 'compose/production/traefik/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'

# Enable version updates for Python/Pip - Production
- package-ecosystem: 'pip'
Expand All @@ -75,4 +75,4 @@ updates:
directory: '/'
# Every weekday
schedule:
interval: 'daily'
interval: 'weekly'
9 changes: 9 additions & 0 deletions concerts/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class Meta:
def get_total_concerts(self, obj):
return obj.concerts.count()

def validate(self, data):
country = data.get("country")

# Check if the country is US and if not, remove the state
if country != "US":
data["state"] = None

return data


class ConcertSerializer(serializers.ModelSerializer):
artist = serializers.HyperlinkedRelatedField(view_name="api:v1:artist-detail", queryset=Artist.objects.all())
Expand Down
9 changes: 9 additions & 0 deletions concerts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ class Meta:
model = Venue
fields = ["name", "city", "state", "country"]
widgets = {"country": CountrySelectWidget(attrs={"class": "form-control"})}

def clean(self):
cleaned_data = super().clean()
country = cleaned_data.get("country")

if country != "US":
cleaned_data["state"] = None

return cleaned_data
17 changes: 12 additions & 5 deletions concerts/tests/features/api.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Feature: API Endpoints
When I request the <entity> list
Then I should receive a list of entities

Examples:
| entity |
| artist |
| venue |
| concert |
Examples:
| entity |
| artist |
| venue |
| concert |

@api
Scenario: Retrieve a user's concert reviews list
Expand All @@ -26,6 +26,13 @@ Feature: API Endpoints
When Alice requests the list of concert reviews for Sam
Then the response should contain an empty list of reviews

Scenario: Create international venue through API
Given I have a registered user Bob
And I am a token authenticated user Bob
When I create a new venue with {"name": "International Venue", "city": "Paris", "country": "FR"}
Then the response status code should be 201
And the state of the created venue should be None

@api
Scenario: Update a user's own concert review
Given I have a registered user Sam
Expand Down
11 changes: 10 additions & 1 deletion concerts/tests/features/steps/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from behave import given, then
from behave import given, step, then
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework.authtoken.models import Token
Expand Down Expand Up @@ -82,6 +82,15 @@ def step_impl(context, status_code):
), f"Expected {status_code} but got {context.response.status_code}"


@step("the state of the created venue should be None")
def step_impl(context):
# Get the latest venue created
venue = Venue.objects.order_by("-created_at").first()

# Check if the state is None
assert venue.state is None, f"Expected venue.state to be None, but got {venue.state}"


def after_scenario(context, _):
User = get_user_model()
User.objects.exclude(username="testuser").delete()
Expand Down
7 changes: 7 additions & 0 deletions concerts/tests/features/steps/webviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,10 @@ def step_impl(context, entity_type, invalid_data):
@then("I should see form errors in the response")
def step_impl(context):
assert "This field is required." in context.response.content.decode(), "Expected form errors, but none were found."


@when("I create an international venue with valid data")
def step_impl(context):
data = {"name": "Test International Venue", "city": "Test City", "country": "FR"}
url = reverse("concerts:venue-create")
context.response = context.client.post(url, data=data)
56 changes: 32 additions & 24 deletions concerts/tests/features/webviews.feature
Original file line number Diff line number Diff line change
@@ -1,70 +1,78 @@
Feature: List Views

Scenario Outline: Authenticated user can view a list of entities
Scenario Outline: Authenticated user can view a list of entities
Given I have a registered user "testuser"
And "testuser" is logged in
And there is a <entity_type> in the system
When I request the list of <entity_type>
Then the response status code should be 200

Examples:
| entity_type |
| concert |
| artist |
| venue |
| entity_type |
| concert |
| artist |
| venue |

Scenario Outline: Authenticated user can create entities
Scenario Outline: Authenticated user can create entities
Given I have a registered user "testuser"
And "testuser" is logged in
When I make a new <entity_type> with valid data
Then the response status code should be 302

Examples:
| entity_type |
| concert |
| artist |
| venue |
| review |
| entity_type |
| concert |
| artist |
| venue |
| review |

Scenario Outline: Authenticated user can view details of entities
Scenario: Authenticated user can create an international venue
Given I have a registered user "testuser"
And "testuser" is logged in
When I create an international venue with valid data
Then the response status code should be 302
And the state of the created venue should be None


Scenario Outline: Authenticated user can view details of entities
Given I have a registered user "testuser"
And "testuser" is logged in
And there is a <entity_type> in the system
When I request details for that <entity_type>
Then the response status code should be 200

Examples:
| entity_type |
| concert |
| artist |
| venue |
| review |
| entity_type |
| concert |
| artist |
| venue |
| review |

Scenario: Authenticated user can attend a concert
Scenario: Authenticated user can attend a concert
Given I have a registered user "testuser"
And "testuser" is logged in
And there is a concert in the system
When I attend the concert
Then the response status code should be 302

Scenario: Authenticated user can unattend a concert
Scenario: Authenticated user can unattend a concert
Given I have a registered user "testuser"
And "testuser" is logged in
And there is a concert in the system
And I have attended the concert
When I unattend the concert
Then the response status code should be 302

Scenario Outline: Authenticated user cannot create entities with invalid data
Scenario Outline: Authenticated user cannot create entities with invalid data
Given I have a registered user "testuser"
And "testuser" is logged in
When I attempt to make a new <entity_type> with data <invalid_data>
Then the response status code should be 200
Then I should see form errors in the response

Examples:
| entity_type | invalid_data |
| concert | { "artist": "", "venue": "" } |
| artist | { "name": "" } |
| venue | { "name": "", "city": "", "state": "", "country": ""}|
| entity_type | invalid_data |
| concert | { "artist": "", "venue": "" } |
| artist | { "name": "" } |
| venue | { "name": "", "city": "", "state": "", "country": ""} |

0 comments on commit 0ea01d4

Please sign in to comment.