# Path: rolf/rolf_main/models.py
# class Category(models.Model):
#     name = models.CharField(max_length=256)
# 
#     class Meta:
#         ordering = ('name',)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return "/category/%d/" % self.id
# 
# class Stage(models.Model):
#     name = models.CharField(max_length=256)
#     deployment = models.ForeignKey(Deployment)
#     recipe = models.ForeignKey(Recipe)
# 
#     class Meta:
#         order_with_respect_to = "deployment"
# 
#     def get_absolute_url(self):
#         return "/stage/%d/" % self.id
# 
#     def all_recipes(self):
#         """ helper to make this available in generic templates """
#         return Recipe.objects.all().exclude(name="").exclude(id=self.recipe.id)
# 
# Path: rolf/rolf_main/tests/mixins.py
# class LoggedInTestMixin(TestCase):
#     def setUp(self):
#         self.u = User.objects.create(username='testuser')
#         self.u.set_password('test')
#         self.u.save()
#         self.client.login(username='testuser', password='test')


from urlparse import urlparse
from django.contrib.auth.models import Group
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from factories import UserFactory, CategoryFactory, ApplicationFactory
from factories import DeploymentFactory, StageFactory
from rolf.rolf_main.models import Category, Stage
from rolf.rolf_main.tests.mixins import LoggedInTestMixin

    def test_plain_getkey(self):
        response = self.c.get("/api/1.0/get_key/")
        assert "<h2>Get API Key</h2>" in response.content

        # no ip specified, so there should be a form
        assert ("Alternatively, you can enter an IP address "
                "here and get a key") in response.content

    def test_key_for_other_ip(self):
        response = self.c.get("/api/1.0/get_key/?ip=128.59.1.1")
        assert ("This key is for the specified IP "
                "Address (128.59.1.1)") in response.content


class DeploymentTests(LoggedInTestMixin, TestCase):
    def setUp(self):
        super(DeploymentTests, self).setUp()
        self.deployment = DeploymentFactory()
        self.stage = StageFactory(deployment=self.deployment)

    def test_delete_stage(self):
        r = self.client.post(reverse('stage_delete', args=(self.stage.pk,)))

        self.assertEqual(r.status_code, 302)
        self.assertEqual(
            urlparse(r.url).path,
            reverse('deployment_detail', args=(self.deployment.pk,)))

        with self.assertRaises(Stage.DoesNotExist):

# Path: rolf/rolf_main/models.py
# class Category(models.Model):
#     name = models.CharField(max_length=256)
# 
#     class Meta:
#         ordering = ('name',)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return "/category/%d/" % self.id
# 
# class Stage(models.Model):
#     name = models.CharField(max_length=256)
#     deployment = models.ForeignKey(Deployment)
#     recipe = models.ForeignKey(Recipe)
# 
#     class Meta:
#         order_with_respect_to = "deployment"
# 
#     def get_absolute_url(self):
#         return "/stage/%d/" % self.id
# 
#     def all_recipes(self):
#         """ helper to make this available in generic templates """
#         return Recipe.objects.all().exclude(name="").exclude(id=self.recipe.id)
# 
# Path: rolf/rolf_main/tests/mixins.py
# class LoggedInTestMixin(TestCase):
#     def setUp(self):
#         self.u = User.objects.create(username='testuser')
#         self.u.set_password('test')
#         self.u.save()
#         self.client.login(username='testuser', password='test')


# Path: rolf/rolf_main/tests/test_views.py
from urlparse import urlparse
from django.contrib.auth.models import Group
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from factories import UserFactory, CategoryFactory, ApplicationFactory
from factories import DeploymentFactory, StageFactory
from rolf.rolf_main.models import Category, Stage
from rolf.rolf_main.tests.mixins import LoggedInTestMixin


    def test_plain_getkey(self):
        response = self.c.get("/api/1.0/get_key/")
        assert "<h2>Get API Key</h2>" in response.content

        # no ip specified, so there should be a form
        assert ("Alternatively, you can enter an IP address "
                "here and get a key") in response.content

    def test_key_for_other_ip(self):
        response = self.c.get("/api/1.0/get_key/?ip=128.59.1.1")
        assert ("This key is for the specified IP "
                "Address (128.59.1.1)") in response.content


class DeploymentTests(LoggedInTestMixin, TestCase):
    def setUp(self):
        super(DeploymentTests, self).setUp()
        self.deployment = DeploymentFactory()
        self.stage = StageFactory(deployment=self.deployment)

    def test_delete_stage(self):
        r = self.client.post(reverse('stage_delete', args=(self.stage.pk,)))

        self.assertEqual(r.status_code, 302)
        self.assertEqual(
            urlparse(r.url).path,
            reverse('deployment_detail', args=(self.deployment.pk,)))

        with self.assertRaises(Stage.DoesNotExist):

            Stage.objects.get(pk=self.stage.pk)