def testGet_InvalidScope(self): with self.LoggedInUser(): response = self.testapp.get( '/alerts/blah/macos', expect_errors=True) self.assertEqual(httplib.BAD_REQUEST, response.status_int) self.assertMemcacheLacks(alerts._CreateMemcacheKey('blah', 'macos'))
def testPost_ExistingAlert(self): now = datetime.datetime.utcnow() start_date_1 = (now - datetime.timedelta(hours=2)).strftime( alerts._DATETIME_FORMAT_STRING) start_date_2 = (now - datetime.timedelta(hours=1)).strftime( alerts._DATETIME_FORMAT_STRING) params = { 'message': 'this is a calm message', 'severity': 'INFO', 'start_date': start_date_1} self.assertEntityCount(alert.Alert, 0) # Create an initial Alert via POST. with self.LoggedInUser(admin=True): response = self.testapp.post(self.ROUTE, params) self.assertEqual(httplib.OK, response.status_int) self.assertEntityCount(alert.Alert, 1) # Populate memcache with a subsequent GET. with self.LoggedInUser(admin=True): response = self.testapp.get(self.ROUTE) alert_dict = alert.Alert.query().get().to_dict() self.assertMemcacheContains( alerts._CreateMemcacheKey('appdetail', 'windows'), alert_dict) params = { 'message': 'THIS IS A SERIOUS MESSAGE', 'severity': 'ERROR', 'start_date': start_date_2} # Ensure that a later POST resets the memcache key. with self.LoggedInUser(admin=True): response = self.testapp.post(self.ROUTE, params) self.assertEqual(httplib.OK, response.status_int) self.assertEntityCount(alert.Alert, 2) self.assertMemcacheLacks(alerts._CreateMemcacheKey('appdetail', 'windows'))
def testGet_InMemcache(self): # Create an active Alert and stuff it in Memcache. alert_dict = _CreateAlert(-5, end_hours=5).to_dict() memcache.set(alerts._CreateMemcacheKey('appdetail', 'windows'), alert_dict) with self.LoggedInUser(): response = self.testapp.get(self.ROUTE) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, alert_dict)
def testGet_InFuture(self): # Create a limited-duration Alert which is upcoming. _CreateAlert(10, end_hours=20).put() self.assertEntityCount(alert.Alert, 1) with self.LoggedInUser(): response = self.testapp.get(self.ROUTE) self.assertEntityCount(alert.Alert, 1) self.assertMemcacheContains( alerts._CreateMemcacheKey('appdetail', 'windows'), {}) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, {})
def testGet_InPast(self): # Create a limited-duration Alert which has already expired. _CreateAlert(-20, end_hours=-10).put() self.assertEntityCount(alert.Alert, 1) with self.LoggedInUser(): response = self.testapp.get(self.ROUTE) self.assertEntityCount(alert.Alert, 0) self.assertMemcacheContains( alerts._CreateMemcacheKey('appdetail', 'windows'), {}) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, {})
def testGet_Active_AllPlatforms(self): # Create an active Alert for all platforms. alert_entity = _CreateAlert( -5, end_hours=5, platform=constants.SITE_ALERT_PLATFORM.ALL) alert_entity.put() alert_dict = alert_entity.to_dict() for platform in constants.SITE_ALERT_PLATFORM.SET_ALL: route = '/alerts/appdetail/%s' % platform with self.LoggedInUser(): response = self.testapp.get(route) self.assertMemcacheContains( alerts._CreateMemcacheKey('appdetail', platform), alert_dict) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, alert_dict)
def testGet_Active_AllScopes(self): # Create an active Alert for all scopes. alert_entity = _CreateAlert( -5, end_hours=5, scope=constants.SITE_ALERT_SCOPE.EVERYWHERE) alert_entity.put() alert_dict = alert_entity.to_dict() for scope in constants.SITE_ALERT_SCOPE.SET_ALL: route = '/alerts/%s/windows' % scope with self.LoggedInUser(): response = self.testapp.get(route) self.assertMemcacheContains( alerts._CreateMemcacheKey(scope, 'windows'), alert_dict) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, alert_dict)
def testGet_Active_IndefiniteDuration(self): # Create an indefinite-duration Alert which is active. alert_entity = _CreateAlert(-5) alert_entity.put() self.assertEntityCount(alert.Alert, 1) with self.LoggedInUser(): response = self.testapp.get(self.ROUTE) alert_dict = alert_entity.to_dict() self.assertEntityCount(alert.Alert, 1) self.assertMemcacheContains( alerts._CreateMemcacheKey('appdetail', 'windows'), alert_dict) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, alert_dict)
def testGet_Active_MultipleOverlapping(self): # Create two active Alerts which overlap at the current time. alert_entity_1 = _CreateAlert(-10) alert_entity_2 = _CreateAlert(-5, end_hours=5) ndb.put_multi([alert_entity_1, alert_entity_2]) self.assertEntityCount(alert.Alert, 2) with self.LoggedInUser(): response = self.testapp.get(self.ROUTE) alert_dict = alert_entity_2.to_dict() self.assertEntityCount(alert.Alert, 2) self.assertMemcacheContains( alerts._CreateMemcacheKey('appdetail', 'windows'), alert_dict) self.assertEqual(httplib.OK, response.status_int) self.assertResponseContains(response, alert_dict)
def testGet_InvalidPlatform(self): with self.LoggedInUser(): response = self.testapp.get('/alerts/appdetail/xbox', expect_errors=True) self.assertEqual(httplib.BAD_REQUEST, response.status_int) self.assertMemcacheLacks(alerts._CreateMemcacheKey('appdetail', 'xbox'))
def testSuccess(self): expected_key = 'alert_appdetail_windows' actual_key = alerts._CreateMemcacheKey( constants.SITE_ALERT_SCOPE.APPDETAIL, platform=constants.SITE_ALERT_PLATFORM.WINDOWS) self.assertEqual(expected_key, actual_key)