Skip to content

Commit

Permalink
Merge pull request #455 from raginirai553/master
Browse files Browse the repository at this point in the history
 final code for kit id removal from account creation process
  • Loading branch information
cassidysymons authored Sep 28, 2022
2 parents b3f7f0c + aff0b0a commit d80ae23
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 85 deletions.
30 changes: 0 additions & 30 deletions microsetta_private_api/api/_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from microsetta_private_api.model.account import Account, AuthorizationMatch
from microsetta_private_api.model.address import Address
from microsetta_private_api.repo.account_repo import AccountRepo
from microsetta_private_api.repo.activation_repo import ActivationRepo
from microsetta_private_api.repo.kit_repo import KitRepo
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.config_manager import SERVER_CONFIG

Expand Down Expand Up @@ -60,39 +58,11 @@ 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)
Expand Down
45 changes: 3 additions & 42 deletions microsetta_private_api/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"first_name": "Jane",
"last_name": "Doe",
"language": "en_US",
KIT_NAME_KEY: EXISTING_KIT_NAME
}
DUMMY_ACCT_INFO_2 = {
"address": {
Expand All @@ -90,7 +89,6 @@
"first_name": "Obie",
"last_name": "Dobie",
"language": "en_US",
KIT_NAME_KEY: EXISTING_KIT_NAME_2
}
DUMMY_ACCT_ADMIN = {
"address": {
Expand Down Expand Up @@ -453,7 +451,6 @@ def _create_dummy_acct_from_t(t, create_dummy_1=True,
input_obj['address']['post_code'],
input_obj['address']['country_code']
),
input_obj['kit_name'],
input_obj['language']
)
else:
Expand Down Expand Up @@ -661,6 +658,7 @@ def validate_dummy_acct_response_body(self, response_obj,
dummy_acct_dict=None):
if dummy_acct_dict is None:
dummy_acct_dict = DUMMY_ACCT_INFO
response_obj.pop('kit_name')

# check expected additional fields/values appear in response body:
# Note that "d.get()" returns none if key not found, doesn't throw err
Expand Down Expand Up @@ -724,34 +722,6 @@ def test_accounts_create_success(self):
# check account id provided in body matches that in location header
self.assertTrue(real_acct_id_from_loc, real_acct_id_from_body)

def test_accounts_create_fail_400_without_required_fields(self):
"""Return 400 validation fail if don't provide a required field """

self.run_query_and_content_required_field_test(
"/api/accounts", "post",
self.default_querystring_dict,
DUMMY_ACCT_INFO,
skip_fields=["kit_name"])

def test_accounts_create_fail_404(self):
"""Return 404 if provided kit name is not found in db."""

# create post input json
input_obj = copy.deepcopy(DUMMY_ACCT_INFO)
input_obj[KIT_NAME_KEY] = MISSING_KIT_NAME
input_json = json.dumps(input_obj)

# execute accounts post (create)
response = self.client.post(
self.accounts_url,
content_type='application/json',
data=input_json,
headers=MOCK_HEADERS
)

# check response code
self.assertEqual(404, response.status_code)

def test_accounts_create_fail_422(self):
"""Return 422 if provided email is in use in db."""

Expand Down Expand Up @@ -911,12 +881,6 @@ def test_account_scrub_success(self):
self.assertEqual(200, response.status_code)
response_obj = json.loads(response.data)

for k in DUMMY_ACCT_INFO:
if k in (KIT_NAME_KEY, 'language'):
continue
self.assertNotEqual(DUMMY_ACCT_INFO[k],
response_obj[k])

# verify deleting is idempotent
response = self.client.delete(
'/api/accounts/%s' %
Expand Down Expand Up @@ -1104,12 +1068,9 @@ def test_account_view_fail_404(self):

# region account update/put tests
@staticmethod
def make_updated_acct_dict(keep_kit_name=False):
def make_updated_acct_dict():
result = copy.deepcopy(DUMMY_ACCT_INFO)

if not keep_kit_name:
result.pop(KIT_NAME_KEY)

result["address"] = {
"city": "Oakland",
"country_code": "US",
Expand All @@ -1124,7 +1085,7 @@ def test_account_update_success(self):
"""Successfully update existing account"""
dummy_acct_id = create_dummy_acct()

changed_acct_dict = self.make_updated_acct_dict(keep_kit_name=True)
changed_acct_dict = self.make_updated_acct_dict()

# create post input json
input_json = json.dumps(changed_acct_dict)
Expand Down
8 changes: 3 additions & 5 deletions microsetta_private_api/api/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def setup_test_data():
12345,
"US"
),
"fakekit",
"en_US")
acct_repo.create_account(acc)

Expand Down Expand Up @@ -664,7 +663,6 @@ def test_create_new_account(self):
"email": FAKE_EMAIL,
"first_name": "Jane",
"last_name": "Doe",
"kit_name": "jb_qhxqe",
"language": "en_US"
})

Expand Down Expand Up @@ -738,13 +736,13 @@ def test_edit_account_info(self):
"email": "[email protected]",
"first_name": "Dan",
"last_name": "H",
"kit_name": "fakekit",
"language": "en_US"
}

# Hard to guess these two, so let's pop em out
acc.pop("creation_time")
acc.pop("update_time")
acc.pop('kit_name')
self.assertDictEqual(acc, regular_data, "Check Initial Account Match")

regular_data.pop("account_id")
Expand All @@ -753,10 +751,8 @@ def test_edit_account_info(self):
# accounts table without changing the email in the authorization causes
# authorization errors (as it should)
the_email = regular_data["email"]
kit_name = regular_data['kit_name']
fuzzy_data = fuzz(regular_data)
fuzzy_data['email'] = the_email
fuzzy_data['kit_name'] = kit_name
fuzzy_data['language'] = regular_data["language"]

# submit an invalid account type
Expand Down Expand Up @@ -788,6 +784,7 @@ def test_edit_account_info(self):
fuzzy_data["account_id"] = "aaaaaaaa-bbbb-cccc-dddd-eeeeffffffff"
acc.pop('creation_time')
acc.pop('update_time')
acc.pop('kit_name')
self.assertDictEqual(fuzzy_data, acc, "Check Fuzz Account Match")

# Attempt to restore back to old data.
Expand All @@ -804,6 +801,7 @@ def test_edit_account_info(self):

acc.pop('creation_time')
acc.pop('update_time')
acc.pop('kit_name')
regular_data['account_type'] = 'standard'
regular_data["account_id"] = "aaaaaaaa-bbbb-cccc-dddd-eeeeffffffff"

Expand Down
2 changes: 2 additions & 0 deletions microsetta_private_api/db/patches/0103.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--new patch to remove the not a null constraint in the account table for column created_with_kit_id
ALTER TABLE ag.account ALTER COLUMN created_with_kit_id DROP NOT NULL;
5 changes: 2 additions & 3 deletions microsetta_private_api/model/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ def from_dict(input_dict, auth_iss, auth_sub):
input_dict['address']['post_code'],
input_dict['address']['country_code']
),
input_dict['kit_name'],
input_dict['language']
)
return result

def __init__(self, account_id, email,
account_type, auth_issuer, auth_sub,
first_name, last_name,
address, created_with_kit_id,
language,
address, language,
created_with_kit_id=None,
creation_time=None, update_time=None):
self.id = account_id
self.email = email
Expand Down
9 changes: 4 additions & 5 deletions microsetta_private_api/repo/account_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, transaction):
"account_type, auth_issuer, auth_sub, " \
"first_name, last_name, " \
"street, city, state, post_code, country_code, " \
"created_with_kit_id, preferred_language"
"preferred_language"

@staticmethod
def _row_to_addr(r):
Expand All @@ -45,8 +45,8 @@ def _row_to_account(r):
r['account_type'], r['auth_issuer'], r['auth_sub'],
r['first_name'], r['last_name'],
AccountRepo._row_to_addr(r),
r['created_with_kit_id'],
r['preferred_language'],
r['created_with_kit_id'],
r['creation_time'], r['update_time'])

@staticmethod
Expand All @@ -55,7 +55,7 @@ def _account_to_row(a):
a.account_type, a.auth_issuer, a.auth_sub,
a.first_name, a.last_name) + \
AccountRepo._addr_to_row(a.address) + \
(a.created_with_kit_id, a.language)
(a.language,)

def claim_legacy_account(self, email, auth_iss, auth_sub):
# Returns now-claimed legacy account if an unclaimed legacy account
Expand Down Expand Up @@ -157,7 +157,6 @@ def update_account(self, account):
"state = %s, "
"post_code = %s, "
"country_code = %s, "
"created_with_kit_id = %s, "
"preferred_language = %s "
"WHERE "
"account.id = %s",
Expand Down Expand Up @@ -186,7 +185,7 @@ def create_account(self, account):
"%s, %s, %s, "
"%s, %s, "
"%s, %s, %s, %s, %s, "
"%s, %s)",
"%s)",
AccountRepo._account_to_row(account))
return cur.rowcount == 1
except psycopg2.errors.UniqueViolation as e:
Expand Down

0 comments on commit d80ae23

Please sign in to comment.