Skip to content

Commit

Permalink
When signing up show user errors earlier in the process.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseshieh committed Apr 27, 2017
1 parent 5d31e36 commit dfdbf7b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
37 changes: 31 additions & 6 deletions gigalixir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,44 @@ def create(ctx, name):

# @create.command()
@cli.command()
@click.option('--email', prompt=True)
@click.option('-p', '--password', prompt=True, hide_input=True, confirmation_prompt=False)
@click.option('--card_number', prompt=True)
@click.option('--card_exp_month', prompt=True)
@click.option('--card_exp_year', prompt=True)
@click.option('--card_cvc', prompt=True)
@click.option('--email')
@click.option('-p', '--password')
@click.option('--card_number')
@click.option('--card_exp_month')
@click.option('--card_exp_year')
@click.option('--card_cvc')
@click.option('-y', '--accept_terms_of_service_and_privacy_policy', is_flag=True)
@click.pass_context
@report_errors
def signup(ctx, email, card_number, card_exp_month, card_exp_year, card_cvc, password, accept_terms_of_service_and_privacy_policy):
"""
Sign up for a new account.
"""
if not accept_terms_of_service_and_privacy_policy:
logging.getLogger("gigalixir-cli").info("GIGALIXIR Terms of Service: https://www.gigalixir.com/terms")
logging.getLogger("gigalixir-cli").info("GIGALIXIR Privacy Policy: https://www.gigalixir.com/privacy")
if not click.confirm('Do you accept the Terms of Service and Privacy Policy?'):
raise Exception("You must accept the Terms of Service and Privacy Policy to continue.")

if email == None:
email = click.prompt('Email')
gigalixir_user.validate_email(ctx.obj['host'], email)

if password == None:
password = click.prompt('Password', hide_input=True)
gigalixir_user.validate_password(ctx.obj['host'], password)

if card_number == None:
logging.getLogger("gigalixir-cli").info("GIGALIXIR Money-Back Guarantee: http://gigalixir.readthedocs.io/en/latest/main.html#money-back-guarantee")
card_number = click.prompt('Credit Card Number', type=int)

if card_exp_month == None:
card_exp_month = click.prompt('Credit Card Expiration Month', type=int)
if card_exp_year == None:
card_exp_year = click.prompt('Credit Card Expiration Year', type=int)
if card_cvc == None:
card_cvc = click.prompt('Credit Card CVC', type=int)

gigalixir_user.create(ctx.obj['host'], email, card_number, card_exp_month, card_exp_year, card_cvc, password, accept_terms_of_service_and_privacy_policy)

@cli.command()
Expand Down
21 changes: 15 additions & 6 deletions gigalixir/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
import json

def create(host, email, card_number, card_exp_month, card_exp_year, card_cvc, password, accept_terms_of_service_and_privacy_policy):
if not accept_terms_of_service_and_privacy_policy:
logging.getLogger("gigalixir-cli").info("GIGALIXIR Terms of Service: https://www.gigalixir.com/terms")
logging.getLogger("gigalixir-cli").info("GIGALIXIR Privacy Policy: https://www.gigalixir.com/privacy")
if not click.confirm('Do you accept the Terms of Service and Privacy Policy?'):
raise Exception("Sorry, you must accept the Terms of Service and Privacy Policy to continue.")

token = stripe.Token.create(
card={
"number": card_number,
Expand All @@ -32,6 +26,21 @@ def create(host, email, card_number, card_exp_month, card_exp_year, card_cvc, pa
if r.status_code == 401:
raise auth.AuthException()
raise Exception(r.text)
logging.getLogger("gigalixir-cli").info('Created account for %s. Confirmation email sent.' % email)

def validate_email(host, email):
r = requests.get('%s/api/validate_email' % host, headers = {
'Content-Type': 'application/json',
}, params = {
"email": email
})
if r.status_code != 200:
errors = json.loads(r.text)["errors"]
raise Exception("\n".join(errors))

def validate_password(host, password):
if len(password) < 4:
raise Exception("Password should be at least 4 characters.")

def change_password(host, email, current_password, new_password):
r = requests.patch('%s/api/users' % host, auth = (email, current_password), json = {
Expand Down
5 changes: 3 additions & 2 deletions test/gigalixir_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

@httpretty.activate
def test_create_user():
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/validate_email', body='{}', content_type='application/json')
httpretty.register_uri(httpretty.POST, 'https://api.stripe.com/v1/tokens', body='{"id":"fake-stripe-token"}', content_type='application/json')
httpretty.register_uri(httpretty.POST, 'https://api.gigalixir.com/api/users', body='{}', content_type='application/json')
runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['signup', '--email=foo@gigalixir.com', '--card_number=4111111111111111', '--card_exp_month=12', '--card_exp_year=34', '--card_cvc=123', '-y'], input="password\n")
assert result.exit_code == 0
expect(httpretty.has_request()).to.be.true
expect(httpretty.httpretty.latest_requests[0].body).to.equal('card%5Bnumber%5D=4111111111111111&card%5Bexp_year%5D=34&card%5Bcvc%5D=123&card%5Bexp_month%5D=12')
expect(httpretty.httpretty.latest_requests[1].body).to.equal('{"stripe_token": "fake-stripe-token", "password": "password", "email": "foo@gigalixir.com"}')
expect(httpretty.httpretty.latest_requests[1].body).to.equal('card%5Bnumber%5D=4111111111111111&card%5Bexp_year%5D=34&card%5Bcvc%5D=123&card%5Bexp_month%5D=12')
expect(httpretty.httpretty.latest_requests[2].body).to.equal('{"stripe_token": "fake-stripe-token", "password": "password", "email": "foo@gigalixir.com"}')

@httpretty.activate
def test_logout():
Expand Down

0 comments on commit dfdbf7b

Please sign in to comment.