def check(check_request: CheckRequest) -> Verdict: user = generate_user() cube = generate_cube() info = generate_info() api = Api(check_request.hostname) try: api.connect() except Exception: return Verdict.DOWN("DOWN") register_pub_cube = api.register(user, cube, info) api.disconnect() if register_pub_cube is None: return Verdict.MUMBLE("Can't register") try: api.connect() except Exception: return Verdict.DOWN("DOWN") login_pub_cube = api.login(user, cube) api.disconnect() if login_pub_cube is None: return Verdict.MUMBLE("Can't login") if register_pub_cube != login_pub_cube: return Verdict.MUMBLE("Wrong public cube") return Verdict.OK()
def put(put_request: PutRequest) -> Verdict: user = generate_user() cube = generate_cube() info = generate_info() api = Api(put_request.hostname) try: api.connect() except Exception: return Verdict.DOWN("DOWN") pub_cube = api.register(user, cube, info) if pub_cube is None: api.disconnect() return Verdict.MUMBLE("Can't register") res = api.encrypt(put_request.flag) if res is None: api.disconnect() return Verdict.MUMBLE("Can't encrypt") ct, priv_key = res api.disconnect() try: is_correct = check_encryption_correctnes(ct, pub_cube, priv_key, put_request.flag) except Exception: is_correct = False if not is_correct: return Verdict.CORRUPT("Wrong encryption") return Verdict.OK(':'.join([user, cube.as_str(), pub_cube.as_str(), ct.decode(), str(priv_key)]))
def get_users_priv_cubes(): user = generate_user() cube = generate_cube() info = generate_info() r = remote(*ADDR) register(r, user, cube, info) users_list = list_users(r) priv_cubes = {} base_cube_inversed = Cube().move("U R' U' F'") for server_user in users_list: if server_user == user: continue server_user_pub_cube = get_users_pub_cube(r, server_user) priv_cubes[server_user] = [ Cube(server_user_pub_cube).multiply(base_cube_inversed.pow(i)) for i in range(7) ] _exit(r) r.close() return priv_cubes
def test_within_month_interval(self): user = utils.generate_user() sessions = utils.generate_sessions_within( user.id, lambda: datetime.timedelta(days=random.randint(0, 30))) query_result = Session.query.last_month(user.id) self.assertEqual(sorted(sessions), sorted(query_result)) self.assertEqual([], Session.query.last_month(-1))
def test_get_similar_profile_refined(): # Loading the Profiles with open("pickles/refined_profiles.pkl", 'rb') as dffp: df = pickle.load(dffp) with open("pickles/vectorized_refined.pkl", 'rb') as vect_dffp: vect_df = pickle.load(vect_dffp) new_profile = generate_user(df) new_profile.to_csv(r"csv/new_profile.csv") print(new_profile.head()) get_similar_profile_refined(df, vect_df, new_profile)
def test_get_user(self): user = utils.generate_user() # get the user response = self.client.get('/api/users/{id}'.format(id=user.id)) json = jsonify(user.to_json()).get_json() self.assertEqual(response.status_code, 200) self.assertEqual(json, response.json) # test the 404 case response = self.client.get('/api/users/id') self.assertEqual(response.status_code, 404)
def put(put_request: PutRequest) -> Verdict: user = generate_user() cube = generate_cube() info = put_request.flag api = Api(put_request.hostname) try: api.connect() except Exception: return Verdict.DOWN("DOWN") pub_cube = api.register(user, cube, info) api.disconnect() if pub_cube is None: return Verdict.MUMBLE("Can't register") return Verdict.OK(':'.join([user, cube.as_str(), pub_cube.as_str()]))
def test_get_user_monthly_sessions(self): user = utils.generate_user() sessions = utils.generate_sessions_within( user.id, lambda: datetime.timedelta(days=random.randint(0, 30))) response = self.client.get( '/api/users/{id}/statistics/monthly'.format(id=user.id)) json = jsonify({ 'id': user.id, 'statistics': { 'monthly': user.monthly_stats.to_json() } }).get_json() self.assertEqual(response.status_code, 200) self.assertEqual(json, response.json)
def get_users_data(): user = generate_user() cube = generate_cube() info = generate_info() r = remote(*ADDR) register(r, user, cube, info) users_list = list_users(r) ciphercubes = {} for server_user in users_list: server_ciphercubes = get_users_ciphercubes(r, server_user) if server_ciphercubes is None: continue server_ciphercubes = [tuple(map(Cube.from_str, x)) for x in loads(b64decode(server_ciphercubes.decode()))] server_pub_cube = get_users_pub_cube(r, server_user) ciphercubes[server_user] = server_pub_cube, server_ciphercubes return ciphercubes
def test_to_json(self): now = datetime.datetime.now() session = utils.generate_session(words=100, chars=200, accuracy=100.0, created_date=now, user_id=1) user = utils.generate_user(id=1, social_id=2, email='*****@*****.**', username='******', last_seen=now) self.assertEqual( { 'id': 1, 'username': '******', 'email': '*****@*****.**', 'last_seen': now, 'sessions': 1, 'words_score': 100, 'chars_score': 200, 'accuracy_score': 100.0 }, user.to_json())
def test_get_user_sessions(self): user = utils.generate_user() # test a user with not sessions response = self.client.get( '/api/users/{id}/sessions'.format(id=user.id)) self.assertEqual(response.status_code, 200) self.assertTrue(len(response.json['sessions']) == 0) self.assertTrue(response.json['count'] == 0) self.assertIsNone(response.json['next']) self.assertIsNone(response.json['prev']) # test with more than 25 (default page limit) sessions = [utils.generate_session(user.id) for _ in range(30)] response = self.client.get( '/api/users/{id}/sessions'.format(id=user.id)) self.assertEqual(response.status_code, 200) self.assertEqual(25, len(response.json['sessions'])) self.assertIsNone(response.json['prev']) self.assertIsNotNone(response.json['next']) self.assertEqual(30, response.json['count']) # test the next url response = self.client.get(response.json['next']) self.assertEqual(response.status_code, 200) self.assertEqual(5, len(response.json['sessions'])) self.assertIsNotNone(response.json['prev']) self.assertIsNone(response.json['next']) self.assertEqual(30, response.json['count']) # test the 404 case response = self.client.get('/api/user/id/sessions') self.assertEqual(response.status_code, 404)
def get(get_request: GetRequest) -> Verdict: user, cube, pub_cube, ct, priv_key = get_request.flag_id.split(":") cube, pub_cube = map(Cube.from_str, (cube, pub_cube)) priv_key = int(priv_key) ct = ct.encode() api = Api(get_request.hostname) user2 = generate_user() cube2 = generate_cube() info2 = generate_info() try: api.connect() except Exception: return Verdict.DOWN("DOWN") if api.register(user2, cube2, info2) is None: api.disconnect() return Verdict.MUMBLE("Can't register") users_list = api.list_users() if not users_list: api.disconnect() return Verdict.MUMBLE("Can't get user's list") if user not in users_list: api.disconnect() return Verdict.MUMBLE("Can't find user in user's list") server_users_pub_cube = api.get_users_pub_cube(user) if server_users_pub_cube is None: api.disconnect() return Verdict.MUMBLE("Can't get user's pub_cube") if server_users_pub_cube != pub_cube: api.disconnect() return Verdict.MUMBLE("Wrong user's pub_cube") server_users_ciphercubes = api.get_users_ciphercubes(user) if server_users_ciphercubes is None: api.disconnect() return Verdict.MUMBLE("Can't get user's ciphercubes") if server_users_ciphercubes != ct: api.disconnect() return Verdict.MUMBLE("Wrong user's ciphercubes") api.disconnect() try: api.connect() except Exception: return Verdict.DOWN("DOWN") login_pub_cube = api.login(user, cube) if login_pub_cube is None: api.disconnect() return Verdict.MUMBLE("Can't login") if login_pub_cube != pub_cube: api.disconnect() return Verdict.MUMBLE("Wrong public cube") res = api.decrypt(priv_key) if res is None: api.disconnect() return Verdict.MUMBLE("Can't decrypt") if res != get_request.flag: api.disconnect() return Verdict.CORRUPT("Wrong decryption") api.disconnect() return Verdict.OK()