示例#1
0
 def test_return_code_from_db(self):
     with Transaction() as t:
         activations = ActivationRepo(t)
         email1 = "*****@*****.**"
         code1 = activations.get_activation_code(email1)
         code2 = activations.get_activation_code(email1)
         self.assertEqual(code1, code2)
         t.rollback()
def generate_activation_codes(body, token_info):
    validate_admin_access(token_info)

    email_list = body.get("emails", [])
    with Transaction() as t:
        activations = ActivationRepo(t)
        map = activations.get_activation_codes(email_list)
        results = [{"email": email, "code": map[email]} for email in map]
        t.commit()
    return jsonify(results), 200
def search_activation(token_info, email_query=None, code_query=None):
    validate_admin_access(token_info)
    with Transaction() as t:
        activations = ActivationRepo(t)
        if email_query is not None:
            infos = activations.search_email(email_query)
        elif code_query is not None:
            infos = activations.search_code(code_query)
        else:
            raise Exception("Must specify an 'email_query' or 'code_query'")

        return jsonify([i.to_api() for i in infos]), 200
示例#4
0
def register_account(body, token_info):
    # First register with AuthRocket, then come here to make the account
    new_acct_id = str(uuid.uuid4())
    body["id"] = new_acct_id
    # Account.from_dict requires a kit_name, even if blank
    kit_name = body.get("kit_name", "")
    body["kit_name"] = kit_name
    code = body.get("code", "")
    body["code"] = code

    account_obj = Account.from_dict(body, token_info[JWT_ISS_CLAIM_KEY],
                                    token_info[JWT_SUB_CLAIM_KEY])

    if kit_name == "" and code == "":
        return jsonify(code=400,
                       message="Account registration requires "
                       "valid kit ID or activation code"), 400

    with Transaction() as t:
        activation_repo = ActivationRepo(t)
        if code != "":
            can_activate, cause = activation_repo.can_activate_with_cause(
                body["email"], code)
            if not can_activate:
                return jsonify(code=404, message=cause), 404
            else:
                activation_repo.use_activation_code(body["email"], code)

        if kit_name != "":
            kit_repo = KitRepo(t)
            kit = kit_repo.get_kit_all_samples(kit_name)
            if kit is None:
                return jsonify(code=404, message="Kit name not found"), 404

        acct_repo = AccountRepo(t)
        acct_repo.create_account(account_obj)
        new_acct = acct_repo.get_account(new_acct_id)
        t.commit()

    response = jsonify(new_acct.to_api())
    response.status_code = 201
    response.headers['Location'] = '/api/accounts/%s' % new_acct_id
    return response
示例#5
0
 def test_code_nondeterminism(self):
     email1 = "*****@*****.**"
     with Transaction() as t:
         activations = ActivationRepo(t)
         code1 = activations.get_activation_code(email1)
         t.rollback()
     with Transaction() as t:
         activations = ActivationRepo(t)
         code2 = activations.get_activation_code(email1)
         t.rollback()
     self.assertNotEqual(code1, code2)
示例#6
0
    def test_activate(self):
        with Transaction() as t:
            activations = ActivationRepo(t)
            email1 = "*****@*****.**"
            email2 = "*****@*****.**"

            code1 = activations.get_activation_code(email1)
            code2 = activations.get_activation_code(email2)

            self.assertFalse(activations.use_activation_code(email1, code2))
            self.assertFalse(activations.use_activation_code(email2, code1))

            self.assertTrue(activations.use_activation_code(email1, code1))
            self.assertTrue(activations.use_activation_code(email2, code2))

            self.assertFalse(activations.use_activation_code(email1, code1))
            self.assertFalse(activations.use_activation_code(email2, code2))
            t.rollback()
def check_activation(email, code):
    with Transaction() as t:
        activation = ActivationRepo(t)
        can, cause = activation.can_activate_with_cause(email, code)
    return jsonify({"can_activate": can, "error": cause}), 200
def send_email(body, token_info):
    validate_admin_access(token_info)

    with Transaction() as t:
        account_id = None
        email = None
        resolution_url = None
        contact_name = None
        activation_code = None
        language = localization.EN_US

        # Depending on issue type, determine what email to send to and
        # what account is involved, as well as what link to send user to
        # address the problem if that is required by the email template
        if body["issue_type"] == "sample":
            # TODO:  Building resolution url is very tricky, and it's not clear
            #  what component should be responsible for doing it.  It requires
            #  knowing what endpoint the client minimal interface is hosted at,
            #  as well as a sample barcode's associated
            #       account_id,
            #       source_id,
            #       sample_id
            #  which generally requires lookup in the db with admin privilege.

            diag = AdminRepo(t).retrieve_diagnostics_by_barcode(
                       body["template_args"]["sample_barcode"],
                       grab_kit=False)
            account_id = None
            email = None
            contact_name = None

            if diag["account"] is not None:
                account_id = diag["account"].id
                email = diag["account"].email
                contact_name = diag["account"].first_name + " " + \
                    diag["account"].last_name
                contact_name = contact_name.strip()

            source_id = None
            if diag["source"] is not None:
                source_id = diag["source"].id

            sample_id = None
            if diag["sample"] is not None:
                sample_id = diag["sample"].id
            endpoint = SERVER_CONFIG["endpoint"]

            if account_id is not None:
                language = diag['account'].language

            if sample_id is not None and \
               source_id is not None and \
               account_id is not None:
                resolution_url = build_login_redirect(
                    endpoint + "/accounts/%s/sources/%s/samples/%s" %
                    (account_id, source_id, sample_id)
                )
            elif account_id is not None and source_id is not None:
                resolution_url = build_login_redirect(
                    endpoint + "/accounts/%s/sources/%s" %
                    (account_id, source_id)
                )
            elif account_id is not None:
                resolution_url = build_login_redirect(
                    endpoint + "/accounts/%s" % (account_id,)
                )
            else:
                resolution_url = build_login_redirect(
                    endpoint + "/"
                )
        elif body["issue_type"] == "activation":
            # If we are sending activation emails, we won't have
            # anything in our database- no account id, no contact name
            # no known email.  All we actually can do is read out the email
            # and append an activation code to the set of arguments
            if body["template"] == EmailMessage.send_activation_code.name:
                email = body['template_args']['new_account_email']
                activations = ActivationRepo(t)
                activation_code = activations.get_activation_code(email)
            else:
                raise Exception("Support more activation subtypes")
        else:
            raise Exception("Update Admin Impl to support more issue types")

        # Determine what template must be sent, and build the template args
        # from whatever is in the body and the resolution url we determined
        template_name = body['template']
        template = EmailMessage[template_name]

        template_args = dict(body['template_args'])
        if resolution_url is not None:
            template_args['resolution_url'] = resolution_url
        if contact_name is not None:
            template_args['contact_name'] = contact_name
        if activation_code is not None:
            template_args['new_account_code'] = activation_code
        celery_send_email.apply_async(args=[email, template_name,
                                            template_args, language])

        # Add an event to the log that we sent this email successfully
        event = LogEvent(
            uuid.uuid4(),
            template.event_type,
            template.event_subtype,
            None,
            {
                # account_id and email are necessary to allow searching the
                # event log.
                "account_id": account_id,
                "email": email,
                "template": body["template"],
                "template_args": body["template_args"]
            })
        EventLogRepo(t).add_event(event)

        t.commit()

    return '', 204