def test_encryption():
    # Define intputs to check
    mobile = "+447740193397"
    password_input = "456"
    password_encrypted = "LfrpS0GQHWrK7Bf3hymo2lY53xZVcIxthjTG92E5s97DpBgwIq0le5CkwxS5gy/r"

    verification_id_input = "5240822173794304"
    verification_id_encrypted = "5RCQVvDLFeIkbOKUkXq4xUfLOUJbF5piAtoLRtvFKhUbtJvvO7wV9cQXhtsQj5jq"

    # Create test object
    encrypt = Encrypt()

    # Test
    print('Test that a key has been loaded')
    assert len(encrypt.key) > 10

    # Test
    print('test encryption without a salt')
    e = encrypt.encryptString(verification_id_input)
    assert verification_id_encrypted == e

    # Test
    print('test encryption with a salt')
    e = encrypt.encryptString(password_input, mobile)
    assert password_encrypted == e

    print("Success")
    def post(self):
        # Step 1 - retrieve and verify user input
        clue = self.request.get('clue').strip()
        mobileNumber = "+44" + self.request.get('mobilenumber').strip()
        password = self.request.get('pass').strip()

        # Step 2 - store the password
        encrypt = Encrypt()
        e = encrypt.encryptString(password, mobileNumber)

        passwordStorerecord = PasswordStore()
        passwordStorerecord.clue = clue
        passwordStorerecord.mobileNumber = mobileNumber
        passwordStorerecord.encryptedPassword = e
        passwordStorerecord.confirmed = 0
        passwordStorerecord.put()

        passwordStoreId = passwordStorerecord.key.id(
        )  # the id of the record just created
        logging.info('storing password id: ' + str(passwordStoreId))

        # Step 3 - store verification record
        verificationRecord = Verification()
        verificationRecord.action = 'add'
        verificationRecord.confirmed = 0
        verificationRecord.passwordStoreId = passwordStoreId
        verificationRecord.put()

        verificationRecordId = verificationRecord.key.id(
        )  # the id of the record just created
        logging.info('storing verification id: ' + str(verificationRecordId))

        # Step 4 - send SMS with encrypted verification
        i = str(verificationRecordId)
        e = encrypt.encryptString(i)
        d = encrypt.decryptString(e)

        sms = SendSMS()
        sms.verifyPasswordAdd(mobileNumber, e)

        logging.info('sending verification: ' + " - " + i + " - " + e + " - " +
                     d)

        # Step 5 - render reply

        template_values = {'nav': 'store', 'id': e}

        template = JINJA_ENVIRONMENT.get_template(
            'templates/check_phone_success.html')
        self.response.write(template.render(template_values))
    def get(self):
        if isDev(self.request.host):
            logging.info("creating test password records")
            for i in xrange(1, 11):
                created = datetime.now() - timedelta(days=i)

                mobileNumber = "+" + str(447700000000 + i)
                clue = "clue" + str(i)
                password = "******" + str(i)
                encrypt = Encrypt()
                e = encrypt.encryptString(password, mobileNumber)

                passwordStorerecord = PasswordStore()
                passwordStorerecord.clue = clue
                passwordStorerecord.mobileNumber = mobileNumber
                passwordStorerecord.encryptedPassword = e
                passwordStorerecord.confirmed = random.choice([0, 1])
                passwordStorerecord.created = created
                passwordStorerecord.put()

            logging.info("creating test verification records")
            for i in xrange(1, 11):
                created = datetime.now() - timedelta(days=i)
                verificationRecord = Verification()

                verificationRecord.action = random.choice(['delete', 'add'])
                verificationRecord.confirmed = random.choice([0, 1])
                verificationRecord.passwordStoreId = 1000 + i * 17
                verificationRecord.created = created
                verificationRecord.put()

            self.response.write("done")

        logging.warning("Tried to activate CleanUpTest not in dev")
    def post(self):
        # Step 1 - retrieve and verify user input
        clue = self.request.get('clue').strip()
        mobileNumber = "+44" + self.request.get('mobilenumber').strip()

        # Step 2 - get the password record

        query = PasswordStore.query(
            ndb.AND(PasswordStore.clue == clue, PasswordStore.mobileNumber ==
                    mobileNumber)).order(-PasswordStore.created)
        passwordStorerecord = query.fetch(1)

        if passwordStorerecord:
            #logging.info('found: ' + clue + " - " + mobileNumber)

            passwordStorerecord = passwordStorerecord[0]
            passwordStoreId = passwordStorerecord.key.id(
            )  # the id of the record just created

            # Step 3 - store verification record
            verificationRecord = Verification()
            verificationRecord.action = 'delete'
            verificationRecord.confirmed = 0
            verificationRecord.passwordStoreId = passwordStoreId
            verificationRecord.put()

            verificationRecordId = verificationRecord.key.id(
            )  # the id of the record just created
            logging.info('storing verification id: ' +
                         str(verificationRecordId))

            # Step 4 - send SMS with encrypted verification

            encrypt = Encrypt()

            i = str(verificationRecordId)
            e = encrypt.encryptString(i)
            d = encrypt.decryptString(e)

            sms = SendSMS()
            sms.verifyDelete(mobileNumber, e)

            logging.info('sending delete verification: ' + " - " + i + " - " +
                         e + " - " + d)
        #else:
        #logging.info('not found: ' + clue + " - " + mobileNumber)

        # Step 5 - render reply
        template_values = {'nav': 'delete', 'id': e}

        template = JINJA_ENVIRONMENT.get_template(
            'templates/check_phone_success.html')
        self.response.write(template.render(template_values))