def create_login_token(self, is_endless=False, ip_address='', user_agent=''): from apps.accounts.models import Token return Token.create(self, is_endless=is_endless, ip_address=ip_address, user_agent=user_agent)
def setUp(self): super(TestAPI, self).setUp() self.sp = SchedulePolicy(name=random_string(), repeat_times=1) self.sp.save() self.service = Service(name=random_string(), policy=self.sp) self.service.save() self.token = Token(key="testtoken") self.token.save() self.servicetoken = ServiceTokens(name="testbinding", service_id=self.service, token_id=self.token) self.servicetoken.save() self.service2 = Service(name=random_string(), policy=self.sp) self.service2.save() self.token2 = Token(key="testtoken2") self.token2.save() self.servicetoken2 = ServiceTokens(name="testbinding", service_id=self.service2, token_id=self.token2) self.servicetoken2.save()
class TestAPI(BaseTestCase): def setUp(self): super(TestAPI, self).setUp() self.sp = SchedulePolicy(name=random_string(), repeat_times=1) self.sp.save() self.service = Service(name=random_string(), policy=self.sp) self.service.save() self.token = Token(key="testtoken") self.token.save() self.servicetoken = ServiceTokens(name="testbinding", service_id=self.service, token_id=self.token) self.servicetoken.save() self.service2 = Service(name=random_string(), policy=self.sp) self.service2.save() self.token2 = Token(key="testtoken2") self.token2.save() self.servicetoken2 = ServiceTokens(name="testbinding", service_id=self.service2, token_id=self.token2) self.servicetoken2.save() def tearDown(self): super(TestAPI, self).tearDown() try: self.servicetoken.delete() self.servicetoken2.delete() self.token2.delete() self.token.delete() self.service2.delete() self.service.delete() self.sp.delete() except: pass def test_create_event(self): try: client = APIClient() response = client.post( '/api/create_event', data={ "incident_key": "testing", "service_key": self.token.key, "event_type": "trigger", "description": "test", "details": "test" }, ) self.assertEqual(201, response.status_code) new_instance = Incident.objects.get(incident_key='testing') self.assertEqual("testing", new_instance.incident_key) self.assertEqual(Incident.TRIGGER, new_instance.event_type) self.assertEqual(self.service, new_instance.service_key) finally: pass def test_create_event_fails_with_invalid_key(self): try: client = APIClient() response = client.post( '/api/create_event', data={ "incident_key": "testing", "service_key": "invalid", "event_type": "trigger", "description": "test", "details": "test" }, ) self.assertEqual(403, response.status_code) finally: pass def inject_incident(self): incident = Incident() incident.service_key = self.service incident.event_type = Incident.TRIGGER incident.incident_key = "testing" incident.description = "test" incident.details = "test" incident.occurred_at = timezone.now() incident.save() def test_create_event_different_service(self): self.inject_incident() try: client = APIClient() response = client.post( '/api/create_event', data={ "incident_key": "testing", "service_key": self.token2.key, "event_type": "trigger", "description": "test", "details": "test" }, ) self.assertEqual(201, response.status_code) incidents = Incident.objects.all() self.assertEqual(2, incidents.count()) finally: pass def test_incident_recovery(self): self.inject_incident() try: client = APIClient() response = client.post( '/api/create_event', data={ "incident_key": "testing", "service_key": self.token.key, "event_type": "resolve", "description": "test", "details": "test" }, ) self.assertEqual(201, response.status_code) updated = Incident.objects.get(incident_key='testing') self.assertEqual(Incident.RESOLVE, updated.event_type) finally: updated.delete() def test_incident_acknowledge(self): self.inject_incident() try: client = APIClient() response = client.post( '/api/create_event', data={ "incident_key": "testing", "service_key": self.token.key, "event_type": "acknowledge", "description": "test", "details": "test" }, ) self.assertEqual(201, response.status_code) updated = Incident.objects.get(incident_key='testing') self.assertEqual(Incident.ACKNOWLEDGE, updated.event_type) finally: updated.delete()
def test_token___unicode__str__(): token = Token() token.save() assert token.key == token.__str__()
def test_token_save(): token = Token() token.save() assert token.key assert len(token.key) == 40, "Should return a 40 charachers key"
def test_token_generate_key(): token = Token() key = token.generate_key() assert key assert len(key) == 40, "Should return a 40 charachers string: hexdigest()"