def _authentication(self): find_result = User.find(phone_number=self.login.phone_number, validate=True) if not find_result[0]: return find_result if len(find_result[1]) == 0: params = {Keys.PHONE_NUMBER: self.login.phone_number} fail = WrongPassOrPhoneNumber( status=HttpStatus.NOT_FOUND, message=Result.language.WRONG_PHONE_NUMBER_OR_PASSWORD, params=params) return False, fail verified_pass = pbkdf2_sha256.verify(self.login.password, find_result[1][0].password) if not verified_pass: params = {Keys.PHONE_NUMBER: self.login.phone_number} fail = WrongPassOrPhoneNumber( status=HttpStatus.NOT_FOUND, message=Keys.WRONG_PHONE_NUMBER_OR_PASSWORD, params=params) return False, fail return True, find_result[1][0]
def test_false_regex_password_3(self): data = { Keys.PASSWORD: "******", Keys.TYPE_USER: Keys.BUSINESS_OWNER, Keys.PHONE_NUMBER: '09125200100' } json_obj = json.dumps(data) with self.app as client: with client.session_transaction() as sess: sess[Keys.FORGET_PASS] = {} sess[Keys.FORGET_PASS][Keys.STATE_ID] = States.PASSWORD # sess[Keys.FORGET_PASS][Keys.CODE] = data[Keys.PASSWORD] sess[Keys.FORGET_PASS][Keys.PHONE_NUMBER] = data[Keys.PHONE_NUMBER] sess[Keys.FORGET_PASS][Keys.TYPE_USER] = "BusinessOwner" # sess.update({Keys.FORGET_PASS: {Keys.STATE_ID: States.PASSWORD}}) response = self.make_post_request(json_obj) dct = json.loads(response.data) self.assertEqual(dct[Keys.STATUS], 400) self.assertEqual(dct[Keys.MESSAGE], Result.language.BAD_SCHEMA) # self.assertEqual(dct[Keys.PARAMS], None) self.assertEqual(session[Keys.FORGET_PASS][Keys.STATE_ID], States.PASSWORD) _, iws = User.find(phone_number=data[Keys.PHONE_NUMBER]) print iws # dct = json.loads(response.data) result = pbkdf2_sha256.verify(data[Keys.PASSWORD], iws[0].password) self.assertFalse(result)
def handle_state_phone_number(self): session[Keys.FORGET_PASS][Keys.USER_ID] = self.phone_number user = User.find(phone_number=session[Keys.FORGET_PASS][Keys.USER_ID]) if not user[0]: dct = user[1].dictionary_creator() return self.serialize(dct, self.converter) if len(user[1]) == 0: return self.create_no_user_dct( Result.language.NOT_REGISTERED_BEFORE) sms = SMSCodeValidation(user[1][0].name, self.phone_number, SMSCodeValidation.create_random_code()) _, code = sms.send_validation_code() result = self.create_success_message( Result.language.CODE_SEND_SUCCESSFULLY) dct = result.dictionary_creator() session.update({ Keys.FORGET_PASS: { Keys.STATE_ID: States.CODE, Keys.CODE: str(code), Keys.USER_ID: self.phone_number, Keys.TYPE_USER: self.type_user, Keys.NAME: user[1][0].name } }) return self.serialize(dct, self.converter)
def test_true_send_password(self): data = { Keys.PASSWORD: "******", Keys.PHONE_NUMBER: '09125200100', Keys.TYPE_USER: Keys.BUSINESS_OWNER } json_obj = json.dumps(data) with self.app as client: with client.session_transaction() as sess: sess[Keys.FORGET_PASS] = {} sess[Keys.FORGET_PASS][Keys.STATE_ID] = States.PASSWORD sess[Keys.FORGET_PASS][Keys.CODE] = data[Keys.PASSWORD] sess[Keys.FORGET_PASS][Keys.PHONE_NUMBER] = data[Keys.PHONE_NUMBER] sess[Keys.FORGET_PASS][Keys.USER_ID] = data[Keys.PHONE_NUMBER] sess[Keys.FORGET_PASS][Keys.TYPE_USER] = Keys.BUSINESS_OWNER # sess.update({Keys.FORGET_PASS: {Keys.STATE_ID: States.PASSWORD}}) response = self.make_post_request(json_obj) dct = json.loads(response.data) self.assertEqual(dct[Keys.STATUS], 200) self.assertEqual(dct[Keys.MESSAGE], Result.language.PASSWORD_CHANGE_SUCCESSFULLY) self.assertEqual(dct[Keys.PARAMS], None) # self.assertEqual(session[Keys.FORGET_PASS][Keys.STATE_ID], "") _, iws = User.find(phone_number=data[Keys.PHONE_NUMBER]) # dct = json.loads(response.data) result = pbkdf2_sha256.verify(data[Keys.PASSWORD], iws[0].password) self.assertTrue(result)
def _push_notification_done_payment(self): loaded_car_result = Car.find(id=self.job.car_id) loaded_business_owner_result = User.find(id=self.job.business_owner_id) if not loaded_car_result[0] or not loaded_business_owner_result[0]: logger.warn('push notification failed') return business_owner = loaded_business_owner_result[1][0] car = loaded_car_result[1][0] IWSAnnouncement.send_payment_result_notification(car, business_owner)
def _is_existence(self): ok, result = User.find(id=self.business_owner.business_owner_id) if not ok: return False, result if len(result) == 0 or not isinstance(result[0], BusinessOwner): return False, UserNotFound(status=404, message=MessagesKeys.ID_IS_NOT_IN_DB, params=None) return True, result[0]
def deny_order(supplier): supplier = User.find(id=supplier.id) info = { Keys.USER_TYPE: Keys.DENY_ORDER_REQUEST, Keys.SUPPLIER: { Keys.NAME: User.name, Keys.REG_ID: User.reg_id }, Keys.MESSAGE: Result.language.DENY_ORDER_BY_SUPPLIER, } response = PushNotification.push(info[Keys.SUPPLIER][Keys.REG_ID], info) return response
def _is_registered_before(self): find_results = User.find(code=self.code.code, phone_number=self.code.phone_number) if not find_results[0]: return find_results if len(find_results[1]) == 0: false_code = FalseCodeValidation( status=404, message="karbari ba shomatreye" + self.code.phone_number + " va code " + self.code.code + " not registered", params=None) return False, false_code return find_results
def _authentication(self): find_result = User.find(phone_number=self.phone_number, validate=True) if not find_result[0]: return find_result if not len(find_result[1]): # in eygam ro chek kon not_registered_before = RegisteredBefore(status=404, message=MessagesKeys.NOT_REGISTERED_BEFORE, params=None) return False, not_registered_before verified_pass = pbkdf2_sha256.verify(self.old_password, find_result[1][0].password) if not verified_pass: params = {Keys.PHONE_NUMBER: self.phone_number} fail = WrongPassOrPhoneNumber(status=404, message=MessagesKeys.OLD_PASSWORD_IS_NOT_VALID, params=params) return False, fail return find_result
def execute(self): if not is_time_past(self.start_time, self.finish_time): # TODO make these lines like a decorators # needs refactor... create a decorator function for handling all of serialization. Something like below # class Name: # name = "Amish" # # def create(cls, *args): # return cls(status= status) # name = create(Name, status=404) # name.name # 'Amish' not_valid_time = BadInputFormat( status=400, message=MessagesKeys.TIME_IS_NOT_CORRECT, params=None) dct = not_valid_time.dictionary_creator() return self.serialize(dct, self.converter) if not is_start_time_before_finish_time(self.start_time): not_valid_time = BadInputFormat( status=400, message=MessagesKeys.TIME_IS_NOT_CORRECT, params=None) dct = not_valid_time.dictionary_creator() return self.serialize(dct, self.converter) error_free, result = User.find(id=self.business_owner_id) if not error_free: dct = result.dictionary_creator() return self.serialize(dct, self.converter) if not result: not_registered = NotRegisteredBefore( status=404, message=MessagesKeys.NOT_REGISTERED_BEFORE) dct = not_registered.dictionary_creator() return self.serialize(dct, self.converter) res = self.is_free_business_owner() if not res[0]: return res[1] self.business_owner = result[0] insert_res = self.insert_in_db() dct = insert_res[1].dictionary_creator() return self.serialize(dct, self.converter)
def test_false_regex_new_password_and_old_password_2(self): data = { Keys.OLD_PASSWORD: "", # 'phone_number': '09125200780', Keys.TYPE_USER: Keys.BUSINESS_OWNER } data1 = json.dumps(data) with self.app as client: with client.session_transaction() as sess: sess['user_id'] = 1 sess[Keys.PHONE_NUMBER] = '09125200100' response = self.make_post_request(data1) dct = json.loads(response.data) self.assertEqual(dct[Keys.STATUS], 400) self.assertEqual(dct[Keys.MESSAGE], Result.language.BAD_SCHEMA) self.assertEqual(dct[Keys.PARAMS], [Result.language.MISSING_PASSWORD_IN_JSON]) self.assertEqual(session['user_id'], 1) _, iws = User.find(phone_number=session[Keys.PHONE_NUMBER]) # dct = json.loads(response.data) result = pbkdf2_sha256.verify(data[Keys.OLD_PASSWORD], iws[0].password) self.assertFalse(result)
def test_true_regex_info_exist_in_db(self): data = { Keys.OLD_PASSWORD: "******", Keys.NEW_PASSWORD: "******", # "phone_number": '09125200780', Keys.TYPE_USER: Keys.BUSINESS_OWNER } data1 = json.dumps(data) with self.app as client: with client.session_transaction() as sess: sess['user_id'] = 1 sess[Keys.PHONE_NUMBER] = '09125200100' response = self.make_post_request(data1) dct = json.loads(response.data) self.assertEqual(dct[Keys.STATUS], 200) self.assertEqual(dct[Keys.MESSAGE], Result.language.PASSWORD_CHANGE_SUCCESSFULLY) self.assertEqual(dct[Keys.PARAMS], None) self.assertEqual(session['user_id'], 1) _, iws = User.find(phone_number=session[Keys.PHONE_NUMBER]) # dct = json.loads(response.data) result = pbkdf2_sha256.verify(data[Keys.NEW_PASSWORD], iws[0].password) self.assertTrue(result)
def test_old_password_is_not_verified_false_regex_new_password_1(self): data = { Keys.OLD_PASSWORD: "******", Keys.NEW_PASSWORD: "******", # 'phone_number': '09125200780', Keys.TYPE_USER: Keys.BUSINESS_OWNER } data1 = json.dumps(data) with self.app as client: with client.session_transaction() as sess: sess['user_id'] = True sess[Keys.PHONE_NUMBER] = '09125200100' response = self.make_post_request(data1) dct = json.loads(response.data) self.assertEqual(dct[Keys.STATUS], 400) self.assertEqual(dct[Keys.MESSAGE], Result.language.BAD_SCHEMA) self.assertEqual(dct[Keys.PARAMS], {'invalid_params': ['password_is_not_strength']}) self.assertEqual(session['user_id'], 1) _, iws = User.find(phone_number=session[Keys.PHONE_NUMBER]) # dct = json.loads(response.data) result = pbkdf2_sha256.verify(data[Keys.NEW_PASSWORD], iws[0].password) self.assertFalse(result)
def _authentication(self): find_result = User.find(phone_number=self.login.phone_number, validate=True) if not find_result[0]: return find_result if len(find_result[1]) == 0: params = {"phone_number": self.login.phone_number} fail = WrongPassOrPhoneNumber(status=404, message="wrong", params=params) return False, fail verified_pass = pbkdf2_sha256.verify(self.login.password, find_result[1][0].password) if not verified_pass: params = {"phone_number": self.login.phone_number} fail = WrongPassOrPhoneNumber(status=404, message="wrong", params=params) return False, fail return True, find_result[1][0]
def test_old_password_is_not_verified_1(self): data = { Keys.OLD_PASSWORD: "******", Keys.NEW_PASSWORD: "******", # 'phone_number': '09125200780', Keys.TYPE_USER: Keys.BUSINESS_OWNER } data1 = json.dumps(data) with self.app as client: with client.session_transaction() as sess: sess['user_id'] = True sess[Keys.PHONE_NUMBER] = '09125200100' response = self.make_post_request(data1) dct = json.loads(response.data) print dct self.assertEqual(dct[Keys.STATUS], 404) self.assertEqual(dct[Keys.MESSAGE], MessagesKeys.OLD_PASSWORD_IS_NOT_VALID) self.assertEqual(dct[Keys.PARAMS], {"phone_number": session[Keys.PHONE_NUMBER]}) self.assertEqual(session['user_id'], 1) _, iws = User.find(phone_number=session[Keys.PHONE_NUMBER]) # dct = json.loads(response.data) result = pbkdf2_sha256.verify(data[Keys.NEW_PASSWORD], iws[0].password) self.assertFalse(result)