def test_in_unassured_collision_no_user_gets_deleted(self): self.doHashCollision(assuredTarget=False) self.assertEqual(self.cred.user.email, User.getByEmail(self.cred.user.email).email) self.assertEqual(self.targetUser.email, User.getByEmail(self.targetUser.email).email) self.assertEqual(self.anotherUser.email, User.getByEmail(self.anotherUser.email).email)
def your_user_is_deleted_in_deregistration(self): with app.test_client() as c: self.login(c) user = User.getByEmail(self.usercreation_email) self.assertTrue(user is not None) data = dict( csrf_token = self.getCSRF(c), credentialType= "password", identifier= self.usercreation_userid, secret = self.usercreation_password ) c.post(config.base_url+'/deregister', data=data) user = User.getByEmail(self.usercreation_email) self.assertTrue(user is None)
def ssl_login_registers_and_logs_in_if_you_have_cert_and_give_email(self): self._keygenAndLogin() self._logoutAfterKeygen() user = User.getByEmail(self.usercreation_email) self.deleteUser(user) sslLoginBaseUrl = app.config.get("SSL_LOGIN_BASE_URL") self.driver.get(sslLoginBaseUrl + '/[email protected]') self.driver.get(sslLoginBaseUrl + '/v1/users/me') body = self.driver.find_element_by_css_selector("BODY").text self.assertTrue('{"credentialType": "certificate", "identifier": "' in body) self.assertTrue('"email": "*****@*****.**"' in body) user = User.getByEmail("*****@*****.**") self.deleteUser(user) self.deleteCerts()
def your_credentials_are_deleted_in_deregistration(self): with app.test_client() as c: self.login(c) user = User.getByEmail(self.usercreation_email) creds = Credential.getByUser(user) self.assertTrue(len(creds) > 0) data = dict( csrf_token = self.getCSRF(c), credentialType= "password", identifier= self.usercreation_userid, secret = self.usercreation_password ) c.post(config.base_url+'/deregister', data=data) user = User.getByEmail(self.usercreation_email) creds = Credential.getByUser(user) self.assertTrue(len(creds) == 0)
def test_users_without_assurer_assurance_cannot_get_user_by_email(self): user = self.createUserWithCredentials() self.assertTrue(user is not None) target = User.getByEmail(self.userCreationEmail) with self.assertRaises(ReportedError) as context: self.controller.doGetByEmail(target.email) self.assertEqual(context.exception.status, 403)
def test_users_with_assurer_assurance_can_get_user_by_email(self): self._createAssurer() self.setupRandom() self.createUserWithCredentials() target = User.getByEmail(self.userCreationEmail) resp = self.controller.doGetByEmail(target.email) self.assertUserResponse(resp)
def test_User_email_with_plus_sign_is_stored_correctly(self): email = "test+{0}@example.com".format(self.randString) user = User.new(email, None) user2 = User.getByEmail(email) self.assertEqual(user2.email, email) self.assertTrue("+" in user2.email) user.rm()
def users_without_assurer_assurance_cannot_get_user_by_email(self): user = self.createUserWithCredentials() self.assertTrue(user is not None) target = User.getByEmail(self.usercreation_email) with self.assertRaises(ReportedError) as e: self.controller.do_get_by_email(target.email) self.assertTrue(e.exception.status,403)
def users_without_login_cannot_get_user_by_email(self): self.controller._testdata.current_user = None self.createUserWithCredentials() target = User.getByEmail(self.usercreation_email) with self.assertRaises(ReportedError) as e: self.controller.do_get_by_email(target.email) self.assertEquals(e.exception.status,403)
def unregistered_user_can_register_with_facebook_in_the_middle_of_login_procedure_of_a_served_application(self): if config.skipFacebookTests: return driver = self.driver self._gotoOauthPage(driver) self.switchToTab('registration') driver.find_element_by_id("Facebook_registration_button").click() time.sleep(1) self.master = driver.current_window_handle timeCount = 1; while (len(driver.window_handles) == 1 ): time timeCount += 1 if ( timeCount > 50 ): break; for handle in driver.window_handles: if handle!=self.master: driver.switch_to.window(handle) driver.find_element_by_id("pass").clear() driver.find_element_by_id("pass").send_keys(config.fbpassword2) driver.find_element_by_id("email").clear() driver.find_element_by_id("email").send_keys(config.fbuser2) driver.find_element_by_id("u_0_2").click() driver.switch_to.window(self.master) time.sleep(5) self.assertTrue(driver.current_url.startswith(self.redirect_uri.lower())) self.user = User.getByEmail(config.fbuser2) Credential.getByUser(self.user, "facebook").rm() self.user.rm()
def if_you_register_without_hash_it_will_be_null(self): with app.test_client() as c: data = self.prepareData() data.pop('digest') c.post(config.base_url + '/v1/register', data=data) user = User.getByEmail(self.registered_email) self.assertEqual(user.hash, None)
def getUserForEmailAndOrHash(self, digest: Digest, email: Union[str,None]): if email: user = User.getByEmail(email) self.checkUserAgainsDigest(digest, user) return user users = User.getByDigest(digest) self.assureExactlyOneUserInList(users) return users[0]
def test_no_by_email_with_wrong_email(self): self._createAssurer() self.setupRandom() self.createUserWithCredentials() target = User.getByEmail(self.userCreationEmail) with self.assertRaises(ReportedError) as context: self.controller.doGetByEmail('u'+target.email) self.assertEqual(context.exception.status,404)
def test_password_reset_creates_password_if_it_does_not_exists(self): form = self.createPasswordResetFormWithSecret() user = User.getByEmail(self.userCreationEmail) passcred = Credential.getByUser(user, "password") passcred.rm() self.controller.doPasswordReset(form) newPassCred = Credential.getByUser(user, "password") self.assertEqual(newPassCred.secret, CredentialManager.protect_secret(self.newPassword))
def test_the_users_hash_is_changed_to_the_new_one(self): with app.test_client() as client: resp = self.login(client) self.assertUserResponse(resp) user = User.getByEmail(self.userCreationEmail) self.assertEqual(user.hash, None) digest = self.createHash() csrf = self.getCSRF(client) data = dict( digest= digest, csrf_token= csrf ) resp = client.post(config.BASE_URL+'/v1/users/me/update_hash', data=data) self.assertEqual(200,resp.status_code) userAfter = User.getByEmail(self.userCreationEmail) self.assertEqual(userAfter.hash, digest)
def users_without_assurer_assurance_cannot_get_email_and_digest_for_anyone(self): current_user = self.controller.getCurrentUser() targetuser=self.createUserWithCredentials() Assurance.new(targetuser,'test',current_user) target = User.getByEmail(self.usercreation_email) with self.assertRaises(ReportedError) as e: self.showUserByCurrentUser(target.userid) self.assertTrue(e.exception.status,403)
def users_with_assurer_assurance_can_get_user_by_email(self): current_user = self.controller.getCurrentUser() Assurance.new(current_user, 'assurer', current_user) self.setupRandom() self.createUserWithCredentials() target = User.getByEmail(self.usercreation_email) resp = self.controller.do_get_by_email(target.email) self.assertUserResponse(resp)
def test_no_by_email_with_wrong_email(self): self._createAssurer() self.setupRandom() self.createUserWithCredentials() target = User.getByEmail(self.userCreationEmail) with self.assertRaises(ReportedError) as context: self.controller.doGetByEmail('u' + target.email) self.assertEqual(context.exception.status, 404)
def _do_get_by_email(self, email): assurances = Assurance.getByUser(self.getCurrentUser()) if assurances.has_key('assurer'): user = User.getByEmail(email) if user is None: raise ReportedError(["no such user"], status=404) return self.as_dict(user) raise ReportedError(["no authorization"], status=403)
def no_password_reset_for_timed_out_temporary_credential(self): password = self.mkRandomPassword() secret = unicode(uuid4()) user = User.getByEmail(self.usercreation_email) Credential.new(user, 'email_for_password_reset', secret, time.time()-1) with app.test_client() as c: data = dict(password=password, secret=secret) resp = c.post("/v1/password_reset", data = data) self.assertEqual(resp.status_code, 404)
def getUserForEmailAndOrHash(self, digest, email): if email: user = User.getByEmail(email) self.deleteDigestFromOtherUsers(user, digest) self.checkUserAgainsDigest(digest, user) return user users = User.getByDigest(digest) self.assureExactlyOneUserInList(users) return users[0]
def test_users_without_assurer_assurance_cannot_get_email_and_digest_for_anyone( self): current_user = self.controller.getCurrentUser() targetuser = self.createUserWithCredentials().user Assurance.new(targetuser, 'test', current_user) target = User.getByEmail(self.userCreationEmail) with self.assertRaises(ReportedError) as context: self.showUserByCurrentUser(target.userid) self.assertEqual(context.exception.status, 403)
def test_users_with_assurer_assurance_can_get_email_and_digest_for_anyone(self): current_user = self._createAssurer() targetuser=self.createUserWithCredentials().user Assurance.new(targetuser,'test',current_user) target = User.getByEmail(self.userCreationEmail) resp = self.showUserByCurrentUser(target.userid) data = self.fromJson(resp) assurances = data['assurances'] self.assertEqual(assurances['test'][0]['assurer'], current_user.email)
def doGetByEmail(self, email): current_user = self.getCurrentUser() assurances = Assurance.getByUser(current_user) if 'assurer' in assurances: user = User.getByEmail(email) if user is None: raise ReportedError([noSuchUser], status=404) return self.as_dict(user) raise ReportedError([noAuthorization], status=403)
def assertEmailChangeIsInitiated(self, resp): text = self.getResponseText(resp) self.assertEqual(200, resp.status_code) self.assertEqual(emailChangeEmailSent, json.loads(text)['message']) user = User.getByEmail(self.userCreationEmail) self.userid=user.userid tempCredential = Credential.getByUser(user, "changeemail") self.secret = tempCredential.secret self.assertEqual(self.newEmail, tempCredential.getAdditionalInfo())
def no_by_email_with_wrong_email(self): current_user = self.controller.getCurrentUser() Assurance.new(current_user, 'assurer', current_user) self.setupRandom() self.createUserWithCredentials() target = User.getByEmail(self.usercreation_email) with self.assertRaises(ReportedError) as e: self.controller.do_get_by_email('u'+target.email) self.assertTrue(e.exception.status,404)
def assertEmailChangeIsInitiated(self, resp): text = self.getResponseText(resp) self.assertEqual(200, resp.status_code) self.assertEqual(emailChangeEmailSent, json.loads(text)['message']) user = User.getByEmail(self.userCreationEmail) self.userid = user.userid tempCredential = Credential.getByUser(user, "changeemail") self.secret = tempCredential.secret self.assertEqual(self.newEmail, tempCredential.getAdditionalInfo())
def removeFbuser(self,user=None): if user is None: user = config.facebookUser2 self.user = User.getByEmail(user.email) if self.user: Credential.getByUser(self.user, "facebook").rm() for appMap in AppMap.getForUser(self.user): appMap.rm() self.user.rm()
def tearDown(self): test_method_name = self._testMethodName TE.driver.save_screenshot("shippable/%s.png" % test_method_name) if os.environ.get("E2EDEBUG"): pdb.set_trace() self.logOut() fbuser = User.getByEmail(config.facebookUser1.email) if fbuser: self.removeFbuser(config.facebookUser1)
def removeFbuser(self, user=None): if user is None: user = config.facebookUser2 self.user = User.getByEmail(user.email) if self.user: Credential.getByUser(self.user, "facebook").rm() for appMap in AppMap.getForUser(self.user): appMap.rm() self.user.rm()
def do_main(verbose, email, assurer_email, assurances): if verbose > 0: print("Setting assurances {0} for user {1} by {2}".format(assurances, email, assurer_email)) user = User.getByEmail(email) if user is None: print "no such user: {0}".format(email) return 2 if assurer_email == 'self': assurer = user else: assurer = User.getByEmail(assurer_email) if user is None: print "no such assurer: {0}".format(assurer_email) return 2 for ass in assurances: if verbose > 1: print("Setting assurance {0} for user {1} by {2}".format(ass, email, assurer_email)) Assurance.new(user, ass, assurer, time.time()) return 0
def test_users_with_assurer_assurance_can_get_email_and_digest_for_anyone( self): current_user = self._createAssurer() targetuser = self.createUserWithCredentials().user Assurance.new(targetuser, 'test', current_user) target = User.getByEmail(self.userCreationEmail) resp = self.showUserByCurrentUser(target.userid) data = self.fromJson(resp) assurances = data['assurances'] self.assertEqual(assurances['test'][0]['assurer'], current_user.email)
def successful_password_reset_sets_the_password(self): password = self.mkRandomPassword() secret = unicode(uuid4()) user = User.getByEmail(self.usercreation_email) Credential.new(user, 'email_for_password_reset', secret, time.time()+3600) with app.test_client() as c: data = dict(password=password, secret=secret) c.post("/v1/password_reset", data = data) cred = Credential.getByUser(user, "password") self.assertEquals(cred.secret, CredentialManager.protect_secret(password))
def successful_password_clears_the_temporary_credential(self): password = self.mkRandomPassword() secret = unicode(uuid4()) user = User.getByEmail(self.usercreation_email) Credential.new(user, 'email_for_password_reset', secret, time.time()+3600) with app.test_client() as c: data = dict(password=password, secret=secret) c.post("/v1/password_reset", data = data) newcred = Credential.get('email_for_password_reset', secret) self.assertEquals(newcred, None)
def test_the_server_can_get_your_user_info_with_your_access_token(self): accessToken = self.obtainAccessToken()['access_token'] url = TE.backendUrl + "/v1/users/me" headers = {'Authorization': 'Bearer {0}'.format(accessToken)} resp = requests.get(url, headers=headers, verify=False) answer = resp.json() user = User.getByEmail(self.userCreationEmail) appMapEntry = AppMap.get(TE.app, user) self.assertEqual(answer['email'], appMapEntry.getEmail()) self.assertEqual(answer['assurances'], [])
def it_is_possible_to_delete_the_hash_by_not_giving_a_digest_in_the_request(self): with app.test_client() as c: resp = self.login(c) self.assertUserResponse(resp) user = User.getByEmail(self.usercreation_email) oldHash = self.createHash() user.hash = oldHash userToCheck = User.getByEmail(self.usercreation_email) self.assertEqual(userToCheck.hash, oldHash) self.setupRandom() csrf = self.getCSRF(c) data = dict( csrf_token= csrf ) resp = c.post(config.base_url+'/v1/users/me/update_hash', data=data) self.assertEqual(200,resp.status_code) userAfter = User.getByEmail(self.usercreation_email) self.assertEqual(userAfter.hash, None)
def getUserForEmailAndOrHash(self, digest: Digest, email: Union[str,None]): if email: user = User.getByEmail(email) if user is None: raise ReportedError([noSuchUser], 400) self.checkUserAgainsDigest(digest, user) return user users = User.getByDigest(digest) self.assureExactlyOneUserInList(users) return users[0]
def do_send_password_reset_email(self, email): user = User.getByEmail(email) if user is None: raise ReportedError(['Invalid email address']) passwordResetEmailExpiration = 14400 secret=unicode(uuid4()) expirationTime = time.time() + passwordResetEmailExpiration Credential.new(user, passwordResetCredentialType, secret, unicode(expirationTime)) self.sendPasswordResetMail(user, secret, expirationTime) return self.simple_response("Password reset email has successfully sent.")
def test_if_the_user_had_a_hash_before__it_is_overwritten(self): with app.test_client() as client: resp = self.login(client) self.assertUserResponse(resp) user = User.getByEmail(self.userCreationEmail) oldHash = self.createHash() user.hash = oldHash userToCheck = User.getByEmail(self.userCreationEmail) self.assertEqual(userToCheck.hash, oldHash) self.setupRandom() digest = oldHash csrf = self.getCSRF(client) data = dict( digest= digest, csrf_token= csrf ) resp = client.post(config.BASE_URL+'/v1/users/me/update_hash', data=data) self.assertEqual(200,resp.status_code) userAfter = User.getByEmail(self.userCreationEmail) self.assertEqual(userAfter.hash, digest)
def doUpdateHashForUser(self, client, assurance="test", addDigest=True, digest=None): resp = self.login(client) self.assertUserResponse(resp) user = User.getByEmail(self.userCreationEmail) Assurance.new(user, assurance, user, time.time()) oldHash = self.createHash() user.hash = oldHash user.save() userToCheck = User.getByEmail(self.userCreationEmail) assurancesBefore = Assurance.getByUser(userToCheck) self.assertEqual(len(assurancesBefore), 1) self.setupRandom() csrf = self.getCSRF(client) data = dict(csrf_token=csrf) if addDigest: if digest is None: data['digest'] = self.createHash() else: data['digest'] = digest resp = client.post(config.BASE_URL + '/v1/users/me/update_hash', data=data) return resp
def do_main(verbose, email, assurer_email, assurances): if verbose > 0: print("Setting assurances {0} for user {1} by {2}".format( assurances, email, assurer_email)) user = User.getByEmail(email) if user is None: print("no such user: {0}".format(email)) return 2 if assurer_email == 'self': assurer = user else: assurer = User.getByEmail(assurer_email) if user is None: print("no such assurer: {0}".format(assurer_email)) return 2 for ass in assurances: if verbose > 1: print("Setting assurance {0} for user {1} by {2}".format( ass, email, assurer_email)) Assurance.new(user, ass, assurer, time.time()) return 0
def test_when_a_hash_is_registered_which_is_already_used_by_another_assured_user___the_user_is_notified_about_the_fact_and_registration_fails(self): anotherUser = self.createUserWithCredentials().user Assurance.new(anotherUser, "test", anotherUser) theHash = self.createHash() anotherUser.hash = theHash form = self.prepareLoginForm(digest=theHash) email = self.userCreationEmail self.assertReportedError(self.controller.doRegistration, [form], 400, [anotherUserUsingYourHash]) self.assertEqual(email, User.getByEmail(email).email)
def test_email_verification_after_expiry_will_fail(self): self.setupRandom() email = self.registerAndObtainValidationUri() with app.test_client() as client: user = User.getByEmail(email) creds = Credential.getByUser(user) for cred in creds: if cred.credentialType == 'emailcheck': cred.identifier = str(time.time() - 1) resp = client.get(self.validateUri) self.assertEqual(400, resp.status_code) self.assertEqual('{"errors": ["expired token"]}', self.getResponseText(resp))
def test_valid_secret_is_accepted(self): password = self.mkRandomPassword() secret = uuid4().hex user = User.getByEmail(self.userCreationEmail) Credential.new(user, 'email_for_password_reset', str(time.time() + 3600), secret) with app.test_client() as client: data = dict(password=password, secret=secret) resp = client.post("/v1/password_reset", data=data) self.assertEqual(resp.status_code, 200) respData = self.fromJson(resp) self.assertEqual("Password successfully changed", respData['message'])
def getCredentialFromForm(cls, form): cred = Credential.get('password', form.identifier.data) if cred is None: user = User.getByEmail(form.identifier.data) if user is None: return None cred = Credential.getByUser(user, "password") if cred is None: return None hashed = cls.protect_secret(form.password.data) if cred.secret == hashed: return cred return None
def test_the_credential_is_actually_deleted(self): with app.test_client() as client: self.login(client) user = User.getByEmail(self.userCreationEmail) credId = self.randString Credential.new(user, "facebook", credId, "testsecret") self.assertTrue(Credential.get("facebook", credId)) data = { "csrf_token": self.getCSRF(client), "credentialType": "facebook", "identifier": credId, } client.post(config.BASE_URL + "/v1/remove_credential", data=data) self.assertFalse(Credential.get("facebook", credId))
def test_in_hash_collision_if_the_other_user_is_hand_assured_the_user_is_not_deleted(self): anotheruser = self.createUserWithCredentials().user digest = self.createHash() anotheruser.hash = digest Assurance.new(anotheruser, "test", anotheruser) anotheruser.save() user = self.createUserWithCredentials().user app = Application.query.first() # @UndefinedVariable AppMap.new(app, user) email = self.userCreationEmail additionalInfo = dict() with self.assertRaises(ReportedError): self.controller.checkHashInOtherUsers(user,additionalInfo,digest) self.assertEqual(email, User.getByEmail(email).email)
def test_if_createUser_is_set__a_new_user_is_created_with_the_cert_and_logged_in(self): with app.test_client() as client: self.data['createUser']=True self.assertEqual(None, User.getByEmail(self.userCreationEmail)) resp = client.post("/v1/ca/signreq", data=self.data, headers=self.headers) self.assertEqual(resp.status_code, 200) cert = self.getCertFromResponse(resp) identifier = self.controller.getIdentifier(cert) resp2 = client.get("/v1/users/me") self.assertEqual(200, resp2.status_code) cred = Credential.get("certificate", identifier) self.assertTrue(cred) self.assertEqual(cred.user.email, self.userCreationEmail) self.deleteUser(cred.user)
def test_a_credential_can_be_deleted(self): with app.test_client() as client: self.login(client) user = User.getByEmail(self.userCreationEmail) credId = self.randString Credential.new(user, "facebook", credId, "testsecret") self.assertTrue(Credential.get("facebook", credId)) data = { "csrf_token": self.getCSRF(client), "credentialType": "facebook", "identifier": credId, } resp = client.post(config.BASE_URL + "/v1/remove_credential", data=data) self.assertEqual(200, resp.status_code) self.assertEqual('{"message": "credential removed"}', self.getResponseText(resp))
def test_you_should_give_valid_identifier_for_credential_deletion(self): with app.test_client() as client: self.login(client) user = User.getByEmail(self.userCreationEmail) credId = self.randString Credential.new(user, "facebook", credId, "testsecret") self.assertTrue(Credential.get("facebook", credId)) data = { "csrf_token": self.getCSRF(client), "identifier": credId+"no", "credentialType": "facebook", } resp = client.post(config.BASE_URL + "/v1/remove_credential", data=data) self.assertEqual(404, resp.status_code) self.assertEqual('{"errors": ["No such credential"]}', self.getResponseText(resp))
def test_email_validation_gives_emailverification_assurance(self): self.setupRandom() with app.test_client(): email = self.registerAndObtainValidationUri() self.assertTrue( self.validateUri.startswith(config.BASE_URL + "/v1/verify_email")) with app.test_client() as client: user = User.getByEmail(email) creds = Credential.getByUser(user) assurances = Assurance.getByUser(user) self.assertTrue(emailVerification not in assurances) resp = client.get(self.validateUri) self.assertEqual(resp.status_code, 200) self.assertEqual(user.email, email) newcreds = Credential.getByUser(user) self.assertEqual(len(creds) - 1, len(newcreds)) assurances = Assurance.getByUser(user) self.assertTrue(assurances[emailVerification] is not None) user.rm()
def test_your_credentials_are_deleted_in_deregistration(self): self._doDeregistrationDoit() user = User.getByEmail(self.userCreationEmail) creds = Credential.getByUser(user) self.assertTrue(len(creds) == 0)
def test_your_user_with_appmap_is_deleted_in_deregistration(self): self.addAppMapToUser = True self._doDeregistrationDoit() user = User.getByEmail(self.userCreationEmail) self.assertTrue(user is None)
def test_your_assurances_are_deleted_in_deregistration(self): self._doDeregistrationDoit() user = User.getByEmail(self.userCreationEmail) assurances = Assurance.getByUser(user) self.assertTrue(len(assurances) == 0)