def test_user_update(self): old_major = Major.objects().create(name="Test_Name", kd_org="Test_KD_ORG") user = User.objects().create(name="John", username="******", npm="12345678", batch="2020", major=old_major) user.name = "Smith" user.username = "******" user.npm = "0123" user.batch = "123" new_major = Major.objects().create(name="New_Major", kd_org="New_KD_ORG") user.major = new_major user.save() user.reload() assert user.name == "Smith" assert user.username == "wock" assert user.npm == "0123" assert user.batch == "123" assert user.major == new_major user.delete() new_major.delete() old_major.delete()
def process_sso_profile(sso_profile): period_name = app.config["ACTIVE_PERIOD"] user_npm = sso_profile["attributes"]["npm"] major_name = sso_profile["attributes"]["study_program"] major_kd_org = sso_profile["attributes"]["kd_org"] major = Major.objects(kd_org=major_kd_org).first() if major is None: major = Major(name=major_name, kd_org=major_kd_org) major.save() period_detail = Period.objects( major_id=major.id, name=period_name, is_detail=True).first() period_not_detail = Period.objects( major_id=major.id, name=period_name, is_detail=False).first() if period_detail is None: if period_not_detail is None: courses, is_detail = scrape_courses(major_kd_org, period_name) if not courses: result = { "err": True, "major_name": major_name } return result else: courses, is_detail = scrape_courses( major_kd_org, period_name, skip_not_detail=True) if courses: period = Period( major_id=major.id, name=period_name, courses=courses, is_detail=is_detail ) period.save() user = User.objects(npm=user_npm).first() if user is None: user = User( name=sso_profile["attributes"]["ldap_cn"], username=sso_profile["username"], npm=user_npm, batch=f"20{user_npm[:2]}", major=major, ) user.save() token = generate_token(user.id, user.major.id) result = { "user_id": str(user.id), "major_id": str(user.major.id), "token": token } return result
def test_major_deletion(self): major = Major.objects().create(name="Test_Name", kd_org="Test_KD_ORG") assert len(Major.objects) == 1 assert major in Major.objects major.delete() assert len(Major.objects) == 0 assert major not in Major.objects
def process_sso_auth(cls, sso_profile) -> Tuple[dict, int]: user_name = sso_profile["username"] period_name = get_app_config("ACTIVE_PERIOD") user = User.objects(username=user_name).first() if user is None: full_name = sso_profile['attributes']['ldap_cn'] user = User(name=full_name, username=user_name) try: user_npm = sso_profile["attributes"]["npm"] major_name = sso_profile["attributes"]["study_program"] major_kd_org = sso_profile["attributes"]["kd_org"] except KeyError: completion_id = uuid.uuid4() user.completion_id = completion_id user.save() return { 'user_name': user_name, 'full_name': full_name, 'completion_id': str(completion_id) }, 201 user.npm = user_npm user.batch = f"20{user_npm[:2]}" major = Major.objects(kd_org=major_kd_org).first() if major is None: major = Major(name=major_name, kd_org=major_kd_org) major.save() user.major = major user.save() if user.completion_id is not None: return { 'user_name': user.username, 'full_name': user.name, 'completion_id': str(user.completion_id) }, 201 major = user.major period = Period.objects(major_id=major.id, name=period_name, is_detail=True).first() token = generate_token(user.id, user.major.id) result = { "user_id": str(user.id), "major_id": str(user.major.id), "token": token } if period is None: result = {**result, "err": True, "major_name": major.name} return result, 200
def generate_random_user_item(cls): return User( name=cls.get_random_string(255), username=cls.get_random_string(64), npm=cls.get_random_string(20), batch=cls.get_random_string(5), major=Major.objects().create( name=cls.get_random_string(256), kd_org=cls.get_random_string(16), ), )
def test_major_creation(self): major = Major.objects().create(name="Test_Name", kd_org="Test_KD_ORG") majors = Major.objects assert len(majors) == 1 assert major in majors fetched_major = majors().first() assert fetched_major.name == "Test_Name" assert fetched_major.kd_org == "Test_KD_ORG" major.delete()
def create_test_user(): major = Major.objects().create(name='Ilmu Komputer (Computer Science)', kd_org='01.00.12.01') user = User.objects().create( name='NAMA USER', username='******', npm='1701234567', batch='2017', major=major, ) token = generate_token(user.id, user.major.id) return user, token
def test_user_fields_validation(self): major = Major.objects().create(name="Name", kd_org="KD_ORG") test_cases = [ { "name": "A" * 300, "username": "******", "npm": "123", "batch": "123", "major": major, }, { "name": "A" * 300, "username": "******" * 100, "npm": "123", "batch": "123", "major": major, }, { "name": "A" * 300, "username": "******" * 100, "npm": "A" * 30, "batch": "123", "major": major, }, { "name": "A" * 300, "username": "******" * 100, "npm": "A" * 30, "batch": "A" * 10, "major": major, }, { "name": "A" * 300, "username": "******" * 100, "npm": "A" * 30, "batch": "A" * 10, "major": 1, }, ] for case in test_cases: with pytest.raises(ValidationError): User( name=case["name"], username=case["username"], npm=case["npm"], batch=case["batch"], major=case["major"], ).validate()
def test_user_deletion(self): user = User.objects().create( name="John", username="******", npm="12345678", batch="2020", major=Major.objects().create(name="Test_Name", kd_org="Test_KD_ORG"), ) assert len(User.objects) == 1 assert user in User.objects user.delete() assert len(User.objects) == 0 assert user not in User.objects
def test_user_creation(self): major = Major.objects().create(name="Test_Name", kd_org="Test_KD_ORG") User.objects().create(name="John", username="******", npm="12345678", batch="2020", major=major) users = User.objects assert len(users) == 1 fetched_user = users.first() assert fetched_user.name == "John" assert fetched_user.username == "wick" assert fetched_user.npm == "12345678" assert fetched_user.batch == "2020" assert fetched_user.major == major fetched_user.delete() major.delete()
def process_auth_completion(cls, data: AuthCompletionData) -> dict: user = User.objects(completion_id=data.completion_id).first() period_name = get_app_config("ACTIVE_PERIOD") if user is None: raise UserNotFound() base_kd_org_data = get_app_config("BASE_KD_ORG") try: kd_org_data = base_kd_org_data[data.kd_org] except KeyError: raise KdOrgNotFound() major = Major.objects(kd_org=data.kd_org).first() if major is None: major = Major(name=kd_org_data["study_program"], kd_org=data.kd_org) major.save() user.npm = data.npm user.major = major user.completion_id = None user.batch = f"20{data.npm[:2]}" user.save() period = Period.objects(major_id=major.id, name=period_name, is_detail=True).first() token = generate_token(user.id, user.major.id) result = { "user_id": str(user.id), "major_id": str(major.id), "token": token } if period is None: result = {**result, "err": True, "major_name": major.name} return result
def upload(profile): if request.method == "POST": if "file" not in request.files: flash("File error.") return redirect(url_for_custom("router_uploader.upload")) file_ = request.files["file"] if file_.filename == "": flash("File kosong.") return redirect(url_for_custom("router_uploader.upload")) if file_ and allowed_file(file_.filename): if not os.path.isdir(app.config["UPLOAD_FOLDER"]): os.mkdir(app.config["UPLOAD_FOLDER"]) html = file_.read() period, kd_org = get_period_and_kd_org(html) role = check_uploader(profile["npm"]) if (period == app.config["ACTIVE_PERIOD"]) and ( kd_org == profile["kd_org"] or role == "admin"): courses = create_courses(html, is_detail=True) if not courses: flash("Error, hubungi admin. Sertakan file ini.") return redirect(url_for_custom("router_uploader.upload")) major = Major.objects(kd_org=kd_org).first() if major is None: flash("Login susun jadwal beneran dulu ya.") return redirect(url_for_custom("router_uploader.upload")) instance = Period.objects(major_id=major.id, name=period, is_detail=True).first() if instance: instance.courses = courses else: instance = Period(major_id=major.id, name=period, courses=courses, is_detail=True) instance.save() timestamp = int(time.time()) filename = f"{kd_org}_{timestamp}_{secure_filename(file_.filename)}" file_.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) else: flash("Periode salah atau jurusan tidak sesuai.") return redirect(url_for_custom("router_uploader.upload")) flash("Berhasil..!!") return redirect(url_for_custom("router_uploader.upload")) flash("Gagal. File salah atau hubungi admin.") return redirect(url_for_custom("router_uploader.upload")) return render_template("upload.html", profile=profile)