def testUserMissingOneCert(self): user = api.get_user_by_username('bobjones2019') cert1 = api.get_certs()[0] cert2 = api.get_certs()[1] lab = api.get_labs()[0] data = urlencode({'cert': cert1['id'], 'redirect_url': '/labs/'}) self.client.post('/api/labs/' + str(lab['id']) + '/certificates/', data=data, content_type="application/x-www-form-urlencoded") data = urlencode({'cert': cert2['id'], 'redirect_url': '/labs/'}) self.client.post('/api/labs/' + str(lab['id']) + '/certificates/', data=data, content_type="application/x-www-form-urlencoded") data = urlencode({ 'redirect_url': '/users', 'user': user.username, 'role': 0 }) self.client.post('/api/labs/' + str(lab['id']) + '/users/', data=data, content_type="application/x-www-form-urlencoded") api.update_or_create_user_cert(user_id=user.id, cert_id=cert1['id'], cert_file='testCert.pdf', completion_date="2019-07-03", expiry_date="2019-07-03") missingCerts = api.get_missing_lab_certs(user.id, lab['id']) self.assertEqual(missingCerts[0]['name'], cert2['name'])
def test2Certs1Expired(self): data = urlencode({ 'name': 'testexpire', 'expiry_in_years': 1, 'redirect_url': '/certificates/' }) self.client.post('/api/certificates/', content_type="application/x-www-form-urlencoded", data=data) certexpired = list( filter(lambda x: x['name'] == 'testexpire', api.get_certs()))[0] cert = list( filter(lambda x: x['name'] != 'testexpire', api.get_certs()))[0] user = api.get_user_by_username('admin') api.update_or_create_user_cert(user_id=user.id, cert_id=certexpired['id'], cert_file='testCert.pdf', completion_date="2017-07-03", expiry_date="2018-07-03") api.update_or_create_user_cert(user_id=user.id, cert_id=cert['id'], cert_file='testCert.pdf', completion_date="2018-07-03", expiry_date="2018-07-03") expiredCerts = api.get_expired_certs(user.id) certsAll = api.get_user_certs(user.id) self.assertEqual(expiredCerts[0]['name'], 'testexpire') self.assertEqual(len(certsAll), 2)
def testAddCertToUser(self): cert = api.get_certs()[0] user = api.get_user_by_username('admin') api.update_or_create_user_cert(user_id=user.id, cert_id=cert['id'], cert_file='testCert.pdf', completion_date="2019-07-03", expiry_date="2019-07-03") userCert = api.get_user_certs(user.id) self.assertEqual(userCert[0]['cert'], cert['id'])
def testAddSameCertTwice(self): user = api.get_user_by_username('admin') cert = api.get_certs()[0] api.update_or_create_user_cert(user_id=user.id, cert_id=cert['id'], cert_file='testCert.pdf', completion_date="2017-07-03", expiry_date="2018-07-03") api.update_or_create_user_cert(user_id=user.id, cert_id=cert['id'], cert_file='testCert.pdf', completion_date="2017-07-03", expiry_date="2018-07-03") self.assertEqual(len(api.get_user_certs(user.id)), 1)
def post(self, request, *args, **kwargs): # Add a training record user_id = request.POST.get('user') form = self.form_class(request.POST, request.FILES) # Whether form is valid or not if form.is_valid(): data = request.POST files = request.FILES cert = api.get_cert(data['cert']) year = int(data['completion_date_year']) month = int(data['completion_date_month']) day = int(data['completion_date_day']) completion_date = datetime(year=year, month=month, day=day) # Calculate a expiry year expiry_year = year + int(cert['expiry_in_years']) expiry_date = datetime(year=expiry_year, month=month, day=day) result = api.update_or_create_user_cert(data['user'], data['cert'], files['cert_file'], completion_date, expiry_date) # Whether user's certficiate is created successfully or not if result: messages.success(request, 'Success! {0} added.'.format(cert['name'])) res = {'user_id': user_id, 'cert_id': result['cert']} else: messages.error(request, "Error! Failed to add a training.") else: errors_data = form.errors.get_json_data() error_message = 'Please check your inputs.' for key in errors_data.keys(): error_code = errors_data[key][0]['code'] if error_code == 'unique_together': error_message = "The certificate already exists. If you wish to update a new training, please delete your old training first." elif error_code == 'invalid_extension': error_message = errors_data[key][0]['message'] elif error_code == 'file_size_limit': error_message = errors_data[key][0]['message'] elif error_code == 'max_length': error_message = errors_data[key][0]['message'] messages.error( request, "Error! Failed to add your training. {0}".format( error_message)) return HttpResponseRedirect(reverse('user_trainings', args=[user_id]))