Skip to content

Commit 07a5757

Browse files
Merge pull request #561 from colts661/ywang_auth_constraint
Added database constraint on AuthRocket
2 parents 2096e14 + 1c5d630 commit 07a5757

File tree

4 files changed

+85
-17
lines changed

4 files changed

+85
-17
lines changed

microsetta_private_api/api/tests/test_api.py

-15
Original file line numberDiff line numberDiff line change
@@ -900,21 +900,6 @@ def test_accounts_legacies_post_success_empty_already_claimed(self):
900900
response_obj = json.loads(response.data)
901901
self.assertEqual(0, len(response_obj))
902902

903-
def test_accounts_legacies_post_fail_422(self):
904-
"""Return 422 if info in db somehow prevents claiming legacy"""
905-
906-
# It is invalid to have one of the auth fields (e.g. sub)
907-
# be null while the other is filled.
908-
create_dummy_acct(create_dummy_1=True, iss=ACCT_MOCK_ISS,
909-
sub=None)
910-
911-
# execute accounts/legacies post (claim legacy account)
912-
url = '/api/accounts/legacies?%s' % self.default_lang_querystring
913-
response = self.client.post(url, headers=MOCK_HEADERS)
914-
915-
# check response code
916-
self.assertEqual(422, response.status_code)
917-
918903
# endregion accounts/legacies post tests
919904

920905

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Add constraint to the ag.account table so that
2+
-- auth_issuer and auth_sub should either both be null or both be non-null
3+
ALTER TABLE ag.account
4+
ADD CONSTRAINT auth_nullable
5+
CHECK (
6+
(auth_issuer IS NULL AND auth_sub IS NULL) OR
7+
(auth_issuer IS NOT NULL AND auth_sub IS NOT NULL)
8+
);

microsetta_private_api/repo/account_repo.py

+6
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ def update_account(self, account):
188188
raise RepoException("Cannot claim more than one account")
189189
# Unknown exception, re raise it.
190190
raise e
191+
except psycopg2.errors.CheckViolation:
192+
raise RepoException(
193+
"Faulty Authorization Status - Contact Admin"
194+
)
191195

192196
def create_account(self, account):
193197
try:
@@ -215,6 +219,8 @@ def create_account(self, account):
215219

216220
# Unknown exception, re raise it.
217221
raise e
222+
except psycopg2.errors.CheckViolation:
223+
raise RepoException("Faulty Authorization Status - Contact Admin")
218224

219225
def delete_account(self, account_id):
220226
with self._transaction.cursor() as cur:

microsetta_private_api/repo/tests/test_account.py

+71-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
from microsetta_private_api.exceptions import RepoException
55
from microsetta_private_api.repo.transaction import Transaction
66
from microsetta_private_api.repo.account_repo import AccountRepo
7+
from microsetta_private_api.model.account import Account
78

89

910
ACCOUNT_ID = '607f6723-c704-4b52-bc26-556a9aec85f6'
1011
BAD_ACCOUNT_ID = 'badbadba-dbad-badb-adba-dbadbadbadba'
1112

13+
AUTH_ACCT_ID = '0004f77e-d3fd-404a-8f5c-3d548a5b0a3f'
1214
ACCT_ID_1 = '7a98df6a-e4db-40f4-91ec-627ac315d881'
1315
DUMMY_ACCT_INFO_1 = {
1416
"address": {
@@ -25,19 +27,21 @@
2527
"language": "en_US",
2628
"kit_name": 'jb_qhxqe',
2729
"consent_privacy_terms": True,
30+
"latitude": 32.8798916,
31+
"longitude": -117.2363115,
32+
"cannot_geocode": False,
2833
"id": ACCT_ID_1
2934
}
3035
ACCT_MOCK_ISS_1 = "MrUnitTest.go"
3136
ACCT_MOCK_SUB_1 = "NotARealSub"
32-
RESULT_LAT = 32.882274018668355
33-
RESULT_LONG = -117.2353976693118
3437

3538

3639
class AccountTests(unittest.TestCase):
3740
def setUp(self):
3841
with Transaction() as t:
3942
ar = AccountRepo(t)
4043
self.untouched = ar.get_account(ACCOUNT_ID)
44+
self.untouched_auth = ar.get_account(AUTH_ACCT_ID)
4145

4246
def test_scrub(self):
4347
with Transaction() as t:
@@ -86,6 +90,71 @@ def test_scrub_no_account(self):
8690
with self.assertRaises(RepoException):
8791
ar.scrub(BAD_ACCOUNT_ID)
8892

93+
def test_faulty_auth_status_insert(self):
94+
# none of the tests commit
95+
# success: both non-null
96+
with Transaction() as t:
97+
ar = AccountRepo(t)
98+
acct_1 = Account.from_dict(
99+
DUMMY_ACCT_INFO_1,
100+
ACCT_MOCK_ISS_1,
101+
ACCT_MOCK_SUB_1
102+
)
103+
self.assertTrue(ar.create_account(acct_1))
104+
105+
# success: both null
106+
with Transaction() as t:
107+
ar = AccountRepo(t)
108+
acct_2 = Account.from_dict(DUMMY_ACCT_INFO_1, None, None)
109+
self.assertTrue(ar.create_account(acct_2))
110+
111+
# fail: sub null
112+
with Transaction() as t:
113+
ar = AccountRepo(t)
114+
acct_3 = Account.from_dict(
115+
DUMMY_ACCT_INFO_1,
116+
ACCT_MOCK_ISS_1,
117+
None
118+
)
119+
with self.assertRaises(RepoException):
120+
ar.create_account(acct_3)
121+
122+
# fail: iss null
123+
with Transaction() as t:
124+
ar = AccountRepo(t)
125+
acct_4 = Account.from_dict(
126+
DUMMY_ACCT_INFO_1,
127+
None,
128+
ACCT_MOCK_SUB_1
129+
)
130+
with self.assertRaises(RepoException):
131+
ar.create_account(acct_4)
132+
133+
def test_faulty_auth_status_update(self):
134+
# none of the tests commit
135+
# success: both non-null
136+
with Transaction() as t:
137+
ar = AccountRepo(t)
138+
self.untouched_auth.auth_issuer = ACCT_MOCK_ISS_1
139+
self.untouched_auth.auth_sub = ACCT_MOCK_SUB_1
140+
self.assertTrue(ar.update_account(self.untouched_auth))
141+
142+
# fail: sub null
143+
with Transaction() as t:
144+
ar = AccountRepo(t)
145+
self.untouched_auth.auth_issuer = ACCT_MOCK_ISS_1
146+
self.untouched_auth.auth_sub = None
147+
with self.assertRaises(RepoException):
148+
ar.update_account(self.untouched_auth)
149+
150+
# fail: iss null
151+
with Transaction() as t:
152+
ar = AccountRepo(t)
153+
self.untouched_auth.auth_issuer = None
154+
self.untouched_auth.auth_sub = ACCT_MOCK_SUB_1
155+
with self.assertRaises(RepoException):
156+
ar.update_account(self.untouched_auth)
157+
89158

90159
if __name__ == '__main__':
91160
unittest.main()

0 commit comments

Comments
 (0)