Exemple #1
0
 def test_find_user_by_day_inactive_user(self):
     '''Testing inactive user with expired certs'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     date = datetime(2019,6,5)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 6)
    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})
Exemple #3
0
 def test_find_users_by_type_14_days_no_users(self):
     '''Testing where no users have certifications expiring in 14 days'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     lab_users, pis = find_users_by_type(users,2)
     self.assertEqual(len(lab_users),0)
     self.assertEqual(pis,None)
Exemple #4
0
 def test_find_users_by_day_multiple_labs_and_certs(self):
     '''Testing users with expired certs in 2 seperate labs'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     date = datetime(2019,6,4)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 6)
Exemple #5
0
 def test_find_users_by_days_user_with_multiple_certs_1_lab(self):
     '''Testing 1 user with 2 expired certifications in 1 lab'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     date = datetime(2019,6,3)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 5)
     self.assertEqual(len(pis[6]),1)
Exemple #6
0
 def test_find_users_by_days_no_users(self):
     '''Testing where there are no users with expired certifications'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     db.fetch_data()
     users = db.get_users()
     date = datetime(2019,5,29)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 0)
Exemple #7
0
 def test_find_users_by_days_user_in_multiple_labs(self):
     '''Testing 1 user with expired certification involved in 2 labs'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     date = datetime(2019,6,2)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 4)
     self.assertEqual(len(pis[4]), 4)
     self.assertEqual(len(pis[5]), 2)
Exemple #8
0
 def test_find_users_by_days_multiple_pis(self):
     '''Testing 1 user with expired certification and 2 pis in lab'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     date = datetime(2019,6,1)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 3)
     self.assertEqual(len(pis[4]), 3)
     self.assertEqual(len(pis[5]), 1)
Exemple #9
0
 def test_find_users_by_days_multiple_users(self):
     '''Testing 2 users with expired certification and 1 pi in lab'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users = db.get_users()
     date = datetime(2019,5,31)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 2)
     self.assertEqual(len(pis[4]), 2)
     self.assertEqual(list(pis.keys())[0],4) # pi is 4
Exemple #10
0
 def test_find_users_by_days_1_user(self):
     '''Testing where there 1 lab user with an expired certification and 1 pi in lab'''
     db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
     users= db.get_users()
     date = datetime(2019,5,30)
     lab_users, pis = find_users_by_days(users,date.date())
     self.assertEqual(len(lab_users), 1)
     self.assertEqual(len(pis[4]), 1)
     self.assertEqual(list(pis.keys())[0],4)
    def test_before_expiry_date(self):
        print('Test: Remind before expiry date')
        db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
        users = db.get_users()
        admin = db.get_admin()

        self.assertEqual(admin, {1, 2, 3})

        # with no expiry date certs
        target_day1 = dt(2014, 8, 15)
        lab_users, pis = find_users_by_days(users, target_day1.date(), 'before')
        self.assertEqual(lab_users, [])

        self.assertEqual(pis[4][11], [])
        self.assertEqual(pis[4][13], [])
        self.assertEqual(pis[4][15], [])

        self.assertEqual(pis[5][11], [])
        self.assertEqual(pis[5][13], [])
        self.assertEqual(pis[5][15], [])

        self.assertEqual(pis[6][11], [])

        # with expiry date certs
        target_day2 = dt(2019, 7, 13)
        lab_users, pis = find_users_by_days(users, target_day2.date(), 'before')
        self.assertEqual(lab_users[0]['id'], 13)
        self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})

        self.assertEqual(pis[4][11], [])
        self.assertEqual(pis[4][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})
        self.assertEqual(pis[4][15], [])

        self.assertEqual(pis[5][11], [])
        self.assertEqual(pis[5][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})
        self.assertEqual(pis[4][15], [])

        self.assertEqual(pis[6][11], [])

        target_day3 = dt(2019, 7, 14)
        lab_users, pis = find_users_by_days(users, target_day3.date(), 'before')
        self.assertEqual(lab_users[0]['id'], 11)
        self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})

        self.assertEqual(pis[4][11], [{'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}])
        self.assertEqual(pis[4][13], [])
        self.assertEqual(pis[4][15], [])

        self.assertEqual(pis[5][11][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})
        self.assertEqual(pis[5][13], [])
        self.assertEqual(pis[4][15], [])

        self.assertEqual(pis[6][11][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})
Exemple #12
0
 def test_find_users_by_type_30_days_1_user(self):
     '''Testing 1 user with certification expiring in 30 days'''
     with open('lfs_lab_cert_tracker/fixtures/test_user_certs.json') as f:
         d = json.load(f)
         dateExpire = datetime.now() + timedelta(days=30)
         dateCompletion = datetime.now() + timedelta(days=30) - timedelta(days=1825)
         data = {
             "model": "lfs_lab_cert_tracker.usercert",
             "pk": 17,
             "fields": {
                 "expiry_date": dateExpire.strftime('%Y-%m-%d'),
                 "cert_id": 14,
                 "user_id": 14,
                 "cert_file": "users/5/certificates/23/certificate_1.jpg",
                 "uploaded_date": "2018-06-11",
                 "completion_date": dateCompletion.strftime('%Y-%m-%d')
             }
         }
         d.append(data)
         json_data = json.dumps(d)
         with open('lfs_lab_cert_tracker/fixtures/test_user_certs2.json', 'w') as g:
             json.dump(d,g)
         subprocess.run('python manage.py loaddata test_user_certs2')
         db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
         db.fetch_data()
         users = db.get_users()
         lab_users, pis = find_users_by_type(users,1)
         self.assertEqual(len(lab_users),1)
         self.assertEqual(len(pis[4]),1)
         dateExpire = datetime.now() + timedelta(days=365)
         dateCompletion = datetime.now() + timedelta(days=365) - timedelta(days=1825)
         data = {
             "model": "lfs_lab_cert_tracker.usercert",
             "pk": 17,
             "fields": {
                 "expiry_date": dateExpire.strftime('%Y-%m-%d'),
                 "cert_id": 14,
                 "user_id": 14,
                 "cert_file": "users/5/certificates/23/certificate_1.jpg",
                 "uploaded_date": "2018-06-11",
                 "completion_date": dateCompletion.strftime('%Y-%m-%d')
             }
         }
         d.append(data)
         json_data = json.dumps(d)
         with open('lfs_lab_cert_tracker/fixtures/test_user_certs2.json', 'w') as g:
             json.dump(d,g)
         subprocess.run('python manage.py loaddata test_user_certs2')
Exemple #13
0
    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})
    def test_after_expiry_date(self):
        print('Test: Remind after expiry date')
        db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE)
        users = db.get_users()
        admin = db.get_admin()

        self.assertEqual(admin, {1, 2, 3})

        target_day1 = dt(2019, 5, 30)
        lab_users, pis = find_users_by_days(users, target_day1.date(), 'after')
        self.assertEqual(lab_users[0]['id'], 15)
        self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[4][11], [])
        self.assertEqual(pis[4][13], [])
        self.assertEqual(pis[4][15][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[5][11], [])
        self.assertEqual(pis[5][13], [])
        self.assertEqual(pis[5][15][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[6][11], [])


        target_day2 = dt(2019, 6, 30)
        lab_users, pis = find_users_by_days(users, target_day2.date(), 'after')
        self.assertEqual(lab_users[0]['id'], 15)
        self.assertEqual(lab_users[0]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(lab_users[0]['certs'][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[4][11], [])
        self.assertEqual(pis[4][13], [])
        self.assertEqual(pis[4][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(pis[4][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[5][11], [])
        self.assertEqual(pis[5][13], [])
        self.assertEqual(pis[5][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(pis[5][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[6][11], [])


        target_day3 = dt(2019, 7, 14)
        lab_users, pis = find_users_by_days(users, target_day3.date(), 'after')
        self.assertEqual(lab_users[0]['id'], 13)
        self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})

        self.assertEqual(lab_users[1]['id'], 15)
        self.assertEqual(lab_users[1]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(lab_users[1]['certs'][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[4][11], [])
        self.assertEqual(pis[4][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})
        self.assertEqual(pis[4][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(pis[4][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[5][11], [])
        self.assertEqual(pis[5][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})
        self.assertEqual(pis[5][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(pis[5][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[6][11], [])

        target_day4 = dt(2020, 1, 1)
        lab_users, pis = find_users_by_days(users, target_day4.date(), 'after')
        self.assertEqual(lab_users[0]['id'], 11)
        self.assertEqual(lab_users[0]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)})
        self.assertEqual(lab_users[0]['certs'][1], {'id': 22, 'name': 'Transportation of Dangerous Goods Class 7 (Radioactivity) Receiving Course for ground', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2016, 10, 1)})
        self.assertEqual(lab_users[0]['certs'][2], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})

        self.assertEqual(lab_users[1]['id'], 13)
        self.assertEqual(lab_users[1]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})

        self.assertEqual(lab_users[2]['id'], 15)
        self.assertEqual(lab_users[2]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(lab_users[2]['certs'][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[4][11][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)})
        self.assertEqual(pis[4][11][1], {'id': 22, 'name': 'Transportation of Dangerous Goods Class 7 (Radioactivity) Receiving Course for ground', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2016, 10, 1)})
        self.assertEqual(pis[4][11][2], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})

        self.assertEqual(pis[4][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})

        self.assertEqual(pis[4][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(pis[4][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[5][11][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)})
        self.assertEqual(pis[5][11][1], {'id': 22, 'name': 'Transportation of Dangerous Goods Class 7 (Radioactivity) Receiving Course for ground', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2016, 10, 1)})
        self.assertEqual(pis[5][11][2], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})

        self.assertEqual(pis[5][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)})

        self.assertEqual(pis[5][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)})
        self.assertEqual(pis[5][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)})

        self.assertEqual(pis[6][11][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)}, {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})
    """ 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()