def test_missing_certs(self): print('Test: Remind missing certs') db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE) users = db.get_users() certs = db.get_certs() lab_users, pis = find_missing_cert_users(users, certs) self.assertEqual(lab_users[0]['id'], 4) self.assertEqual(lab_users[0]['missing_certs'], [1, 2, 3, 5, 15, 16, 20, 22]) self.assertEqual(lab_users[1]['id'], 5) self.assertEqual(lab_users[1]['missing_certs'], [3, 5, 15, 16, 20, 22]) self.assertEqual(lab_users[2]['id'], 6) self.assertEqual(lab_users[2]['missing_certs'], [16, 1, 2, 20]) self.assertEqual(lab_users[3]['id'], 11) self.assertEqual(lab_users[3]['missing_certs'], [2, 5, 15]) self.assertEqual(lab_users[4]['id'], 13) self.assertEqual(lab_users[4]['missing_certs'], [2, 5, 15, 16, 22]) self.assertEqual(lab_users[5]['id'], 15) self.assertEqual(lab_users[5]['missing_certs'], [1, 2, 3, 5, 15, 22]) self.assertEqual(pis[4], {4, 5, 11, 13, 15}) self.assertEqual(pis[5], {4, 5, 11, 13, 15}) self.assertEqual(pis[6], {11, 6})
def test_find_lab_users_and_pis_having_missing_certs(self): ''' Test: find lab users and pis who are having missing certs ''' db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE) users = db.get_users() certs = db.get_certs() lab_users, pis = find_missing_cert_users(users, certs) # sort by ascending order lab_users = sorted(lab_users, key = lambda i: i['id']) pis = dict( sorted(pis.items()) ) self.assertEqual(len(lab_users), 10) self.assertEqual(lab_users[0]['id'], 1) self.assertEqual(lab_users[0]['missing_certs'], [29]) self.assertEqual(lab_users[1]['id'], 4) self.assertEqual(lab_users[1]['missing_certs'], [16, 14, 15]) self.assertEqual(len(pis), 3) self.assertEqual(pis[4], {17, 4, 5}) self.assertEqual(pis[5], {4, 5}) self.assertEqual(pis[6], {6, 7, 8, 9, 20})
""" Send an email to administrators 2 weeks before the expiry date """ print("send email 14days before") lab_users, pis, = find_users_by_type(users, 2) if len(lab_users) > 0 and len(admin) > 0: send_email_to_admin(users, admin, lab_users, DAYS14, type) # Helper functions def contain_cert(certs, cert_id): """ Check whether user's certs contain a cert """ for cert in certs: if cert['id'] == cert_id: return True return False if __name__ == "__main__": db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE) users = db.get_users() certs = db.get_certs() admin = db.get_admin() #send_email_30days_before(users, certs, 'before') #send_email_14days_before(users, certs, admin, 'before') db.close()