Skip to content

Commit ab677f0

Browse files
author
Nicholas
authored
Graph ql creating initial scalars (canada-ca#54)
* Created email address scalar * Working on email scalar testing * Finished testing for email scalar * Fixed regex, renamed test functions
1 parent 41adad6 commit ab677f0

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

api/functions/error_messages.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ def error_user_does_not_exist():
2020

2121
def error_invalid_credentials():
2222
return str("Incorrect email or password")
23+
24+
25+
def scalar_error_type(value_type, value):
26+
return str("Value is not a valid " + str(value_type) + ": " + str(value))
27+
28+
29+
def scalar_error_only_types(value_types, expected_types, value):
30+
return str("Can only validate " + str(value_types) + " as " + str(expected_types) + " but got a: " + str(type(value)))

api/scalars/__init__.py

Whitespace-only changes.

api/scalars/email_address.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import datetime
2+
from re import compile
3+
from graphene.types import Scalar
4+
from graphql.language import ast
5+
from graphql import GraphQLError
6+
7+
from functions.error_messages import *
8+
9+
EMAIL_ADDRESS_REGEX = r'''(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[
10+
\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[
11+
a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][
12+
0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[
13+
\x01-\x09\x0b\x0c\x0e-\x7f])+)\])'''
14+
15+
EMAIL_ADDRESS_REGEX = compile(EMAIL_ADDRESS_REGEX)
16+
17+
18+
class EmailAddress(Scalar):
19+
'''A field whose value conforms to the standard internet email address format as specified in RFC822: https://www.w3.org/Protocols/rfc822/.'''
20+
21+
@staticmethod
22+
def serialize(value):
23+
if not isinstance(value, str):
24+
raise GraphQLError(scalar_error_type("String", value))
25+
26+
if not EMAIL_ADDRESS_REGEX.search(value):
27+
raise GraphQLError(scalar_error_type("email address", value))
28+
29+
return value
30+
31+
@staticmethod
32+
def parse_value(value):
33+
if not isinstance(value, str):
34+
raise GraphQLError(scalar_error_type("String", value))
35+
36+
if not EMAIL_ADDRESS_REGEX.search(value):
37+
raise GraphQLError(scalar_error_type("email address", value))
38+
39+
return value
40+
41+
@staticmethod
42+
def parse_literal(node):
43+
if not isinstance(node, ast.StringValue):
44+
raise GraphQLError(scalar_error_only_types("strings", "email address", str(ast.Type)))
45+
46+
if not EMAIL_ADDRESS_REGEX.search(node.value):
47+
raise GraphQLError(scalar_error_type("email address", node.value))
48+
49+
return node.value
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import sys
2+
import os
3+
4+
import pytest
5+
from graphene.test import Client
6+
from graphql import GraphQLScalarType
7+
from graphql.language import ast
8+
from graphql import GraphQLError
9+
10+
import unittest
11+
12+
# This is the only way I could get imports to work for unit testing. TODO: See if there is a better way!
13+
PACKAGE_PARENT = '..'
14+
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
15+
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
16+
17+
from scalars.email_address import *
18+
19+
20+
class TestEmailAddressScalar(unittest.TestCase):
21+
22+
def test_valid_email_serialize(self):
23+
test_email = 'test.account@canada.ca'
24+
assert EmailAddress.serialize(test_email)
25+
26+
def test_valid_email_parse_value(self):
27+
test_email = "test.account@canada.ca"
28+
assert EmailAddress.parse_value(test_email)
29+
30+
def test_valid_email_parse_literal(self):
31+
assert EmailAddress.parse_literal(ast.StringValue(
32+
value="test.account@canada.ca"
33+
))
34+
35+
def test_invalid_email_serialize_not_email(self):
36+
test_value = 'This Will Fail'
37+
with self.assertRaisesRegex(GraphQLError, scalar_error_type("email address", test_value)):
38+
EmailAddress.serialize(test_value)
39+
40+
def test_invalid_email_serialize_wrong_type(self):
41+
test_value = 1234
42+
with self.assertRaisesRegex(GraphQLError, scalar_error_type("String", test_value)):
43+
EmailAddress.serialize(test_value)
44+
45+
def test_invalid_email_parse_value_not_email(self):
46+
test_value = 'This Will Fail'
47+
with self.assertRaisesRegex(GraphQLError, scalar_error_type("email address", test_value)):
48+
EmailAddress.parse_value(test_value)
49+
50+
def test_invalid_email_parse_value_wrong_type(self):
51+
test_value = 1234
52+
with self.assertRaisesRegex(GraphQLError, scalar_error_type("String", test_value)):
53+
EmailAddress.parse_value(test_value)
54+
55+
def test_invalid_email_parse_literal_not_email(self):
56+
test_value = ast.StringValue(
57+
value='This Will Fail'
58+
)
59+
with self.assertRaisesRegex(GraphQLError, scalar_error_type("email address", test_value.value)):
60+
EmailAddress.parse_literal(test_value)
61+
62+
def test_invalid_email_parse_literal_wrong_ast_type(self):
63+
test_value = ast.IntValue(
64+
value="1234"
65+
)
66+
with self.assertRaisesRegex(GraphQLError, scalar_error_only_types("strings", "email address", str(ast.Type))):
67+
EmailAddress.parse_literal(test_value)

0 commit comments

Comments
 (0)