Ejemplo n.º 1
0
def test_dont_run_next_month_order_subscription_suspended(
        datetime_m1, datetime_m2, app, user_notified_shipped_order_20150530,
        user, user_address):
    datetime_m1.utcnow.return_value = datetime(2017, 5, 30, 13, 13, 13, 13)
    datetime_m1.side_effect = lambda *args, **kwargs: datetime(*args, **kwargs)
    datetime_m2.utcnow.return_value = datetime_m1.utcnow.return_value
    datetime_m2.side_effect = datetime_m1.side_effect
    user_id = user.id

    user.primary_user_subscription.state = SUBSCRIPTION_SUSPENDED_STATE
    db.session.commit()

    manager = OrderManager(user_notified_shipped_order_20150530.id)
    manager.run_action(SET_USER_RECEIVED_ACTION)

    with patch('core.order.order_manager.run_order') as run_order_m:
        # Test
        run_scheduled_orders()

        # Check
        user = get_user(user_id)
        assert len(user.orders) == 2
        next_month_order = [
            o for o in user.orders
            if o.id != user_notified_shipped_order_20150530.id
        ]
        assert len(next_month_order) == 1

        run_order_m.delay.assert_not_called()
Ejemplo n.º 2
0
def test_run_next_month_order_already(datetime_m1, datetime_m2, app,
                                      user_notified_shipped_order_20150530,
                                      user, user_address):
    datetime_m1.utcnow.return_value = datetime(2017, 5, 30, 13, 13, 13, 13)
    datetime_m1.side_effect = lambda *args, **kwargs: datetime(*args, **kwargs)
    datetime_m2.utcnow.return_value = datetime_m1.utcnow.return_value
    datetime_m2.side_effect = datetime_m1.side_effect
    user_id = user.id

    manager = OrderManager(user_notified_shipped_order_20150530.id)
    manager.run_action(SET_USER_RECEIVED_ACTION)

    with patch('core.order.order_manager.run_order') as run_order_m:
        # Test
        run_scheduled_orders()

        # Check
        user = get_user(user_id)
        assert len(user.orders) == 2
        next_month_order = [
            o for o in user.orders
            if o.id != user_notified_shipped_order_20150530.id
        ]
        assert len(next_month_order) == 1

        run_order_m.delay.assert_called_once_with(next_month_order[0].id)
Ejemplo n.º 3
0
def test_save_stripe_customer(stripe_m, graphql_client3, user_no_stripe):
    user_id = user_no_stripe.id
    customer = MagicMock()
    customer.id = 'customer_id'

    stripe_m.Customer.create.return_value = customer
    token = 'stripe_token'

    query = """
        mutation saveUser ($input: SaveUserInput!) 
         { saveUser(input: $input) {clientMutationId} } 
    """
    variables = {
        'input': {
            'clientMutationId': 'test_id',
            'firstName': 'new_test_name',
            'stripeToken': token
        }
    }

    # Test
    graphql_client3.post(query, variables)

    # Check
    _user = get_user(user_id)
    assert _user.stripe_customer_id == customer.id
    stripe_m.Customer.create.assert_called_once_with(
        source=token, email=user_no_stripe.email
    )
Ejemplo n.º 4
0
def test_start_search(send_mail_m, graphql_client, user, user_subscription,
                      user_address):
    user_id = user.id

    mutation = """mutation {
        startSearch(input: {}) {clientMutationId}}"""

    # Test
    graphql_client.post(mutation)

    # Check
    order = db.session.query(Order).filter_by(user_id=user.id).first()
    order_history = db.session.query(OrderHistory).filter_by(
        order_id=order.id).first()
    subscription_snapshot = db.session.query(
        UserSubscriptionSnapshot).filter_by(order_id=order.id).first()

    assert order.state == STARTED_STATE
    assert order_history.state == STARTED_STATE

    user = get_user(user_id)
    for f in ('type', 'bottle_qty', 'budget'):
        assert getattr(subscription_snapshot, f) == \
               getattr(user.primary_user_subscription, f)

    send_mail_m.assert_called_once_with(
        user.email, 'welcome', {
            'name': user.first_name,
            'deeplink': config.SERVER_DEEPLINK_URL + '/subscriptions/',
        })
Ejemplo n.º 5
0
def test_save_stripe_customer_id(stripe_m, app, user_no_stripe):
    customer = MagicMock()
    customer.id = 'customer_id'

    stripe_m.Customer.create.return_value = customer

    # Test
    token = 'test_token'
    save_stripe_customer_id(user_no_stripe.id, token)

    # Check
    _user = get_user(user_no_stripe.id)
    assert _user.stripe_customer_id == customer.id
    stripe_m.Customer.create.assert_called_once_with(
        source=token, email=user_no_stripe.email
    )
Ejemplo n.º 6
0
    def mutate_and_get_payload(cls, root, info, identity, **inp):
        logging.info('saveUser mutation input: %s', inp)

        try:
            global_id = inp['id']
        except KeyError:
            user = get_or_create_user(identity.id.subject)
            is_user_new = not user.registration_finished
        else:
            user_id = from_global_id_assert_type(global_id, 'User')
            user = get_user(user_id)
            is_user_new = False

        if user.cognito_sub == identity.id.subject:
            is_admin_user = admin_user_permission.can()
        else:
            admin_user_permission.test(http_exception=401)
            is_admin_user = True

        old_firstname = user.first_name or ''
        old_lastname = user.last_name or ''
        old_username = user.username or ''
        save_input_fields(
            inp,
            (
                'first_name',
                'last_name',
                'username',
                'avatar',
            ),
            user
        )
        new_firstname = user.first_name or ''
        new_lastname = user.last_name or ''
        new_username = user.username or ''

        if new_username != old_username:
            # TODO otereshchenko: make sure invalid characters are escaped in manual usernames as well
            user.username_set_manually = True

        if new_firstname != old_firstname or new_lastname != old_lastname:
            populate_automatic_username(user)

        # Save address
        save_input_subfields(
            inp,
            'user_address',
            (
                'country',
                'state_region',
                'city',
                'street1',
                'street2',
                'postcode',
            ),
            user,
            lambda: UserAddressModel(user_id=user.id),
            model_attr='primary_user_address'
        )

        # Save subscription
        subscription_inp = inp.get('user_subscription')
        if subscription_inp is not None:
            subscription_inp.produce_budget_decimal()
            subscription = save_subscription(user.id, subscription_inp)
            user.primary_user_subscription_id = subscription.id

        # Save device_token
        device_token_inp = inp.get('device_token')
        if device_token_inp is not None:
            save_device_token(user.id, device_token_inp)

        # Save Stripe customer id
        stripe_token = inp.get('stripe_token')
        if stripe_token is not None:
            save_stripe_customer_id(user.id, stripe_token)

        save_user_cards(inp, user)

        if is_admin_user:
            cognito_user_sync = CognitoUserSync(username=user.cognito_sub)
        else:
            cognito_user_sync = CognitoUserSync(access_token=identity.id.encoded_token)

        cognito_updated = False
        if is_admin_user or not is_user_new:
            cognito_updated = save_cognito_user(cognito_user_sync, inp)

        if cognito_updated or is_user_new:
            try:
                update_user_from_cognito(
                    cognito_user_sync=cognito_user_sync, user=user, clear_missing_data=False
                )
            except:
                logging.exception(
                    'Failed to synchronize a user with id=%s from Cognito to DB!', user.id
                )

        if is_user_new and user.first_name:
            user.registration_finished = True

        db.session.commit()

        return SaveUser(user=user)