Merge pull request #23 from GoatCTF/feature-unique-team-name

[Fix #21] Require unique team names
This commit is contained in:
Louis Fogel 2014-10-31 21:27:44 -04:00
commit c368f46955
2 changed files with 31 additions and 1 deletions

View File

@ -37,7 +37,7 @@ class Challenge(models.Model):
class Team(models.Model):
"""A team is a collection of players."""
name = models.CharField(max_length=TEAM_NAME_LENGTH)
name = models.CharField(max_length=TEAM_NAME_LENGTH, unique=True)
creator = models.ForeignKey("Player", related_name="created_teams")
def __str__(self):

View File

@ -0,0 +1,30 @@
from django.db.utils import IntegrityError
import pytest
from core.models import Player, Team
@pytest.fixture
def user():
user = Player()
user.username = 'user'
user.password = ''
user.save()
return user
@pytest.mark.django_db
def test_cannot_create_without_creator(user):
team1 = Team(name="Team 1")
with pytest.raises(IntegrityError):
team1.save()
@pytest.mark.django_db
def test_team_names_are_unique(user):
team1 = Team(name="Team 1", creator=user)
team1.save()
team2 = Team(name="Team 1", creator=user)
with pytest.raises(IntegrityError):
team2.save()