def post(self, nfc_serial_number, public_serial_number):
        post_data = request.get_json()

        public_serial_number = post_data.get('public_serial_number')
        nfc_serial_number = post_data.get('nfc_serial_number')

        if not public_serial_number or not nfc_serial_number:
            response_object = {
                'message': 'Missing Data',
            }

            return make_response(jsonify(response_object)), 400

        # serial numbers are case insensitive
        public_serial_number = re.sub(r'[\t\n\r]', '',
                                      str(public_serial_number)).upper()
        nfc_serial_number = nfc_serial_number.upper()

        if TransferCard.query.filter_by(
                public_serial_number=public_serial_number).first():
            response_object = {
                'message': 'Public Serial Number already used',
            }

            return make_response(jsonify(response_object)), 400

        if TransferCard.query.filter_by(
                nfc_serial_number=nfc_serial_number).first():
            response_object = {
                'message': 'NFC Serial Number already used',
            }

            return make_response(jsonify(response_object)), 400

        transfer_card = TransferCard(nfc_serial_number=nfc_serial_number,
                                     public_serial_number=public_serial_number)

        db.session.add(transfer_card)
        db.session.commit()

        response_object = {
            'status': 'success',
        }

        return make_response(jsonify(response_object)), 201
Esempio n. 2
0
def test_create_user(test_client, authed_sempo_admin_user, init_database, create_transfer_account_user,
                     mock_async_set_user_gps_from_location, user_phone_accessor, phone, use_card,
                     business_usage_name, referred_by, gps_location, initial_disbursement, tier, status_code):

    if tier:
        authed_sempo_admin_user.set_held_role('ADMIN', tier)
        auth = get_complete_auth_token(authed_sempo_admin_user)
    else:
        auth = None

    # create the user who is referring
    create_transfer_account_user.phone = referred_by
    user_phone_accessor(create_transfer_account_user)

    payload = {
            'first_name': 'John',
            'last_name': 'Smith',
            'bio': 'EasyMart',
            'gender': 'female',
            'phone': phone,
            'initial_disbursement': initial_disbursement,
            'location': 'Elwood',
            'business_usage_name': business_usage_name,
            'referred_by': user_phone_accessor(create_transfer_account_user)
        }

    if gps_location:
        payload['gps_location'] = gps_location

    if use_card:
        public_serial_number = f'{randint(0,999999):06}'
        new_card = TransferCard(public_serial_number=public_serial_number)

        init_database.session.add(new_card)

        init_database.session.commit()

        payload['public_serial_number'] = public_serial_number

    response = test_client.post(
        "/api/v1/user/",
        headers=dict(
            Authorization=auth,
            Accept='application/json'
        ),
        json=payload)
    
    assert response.status_code == status_code
    if response.status_code == 200:
        data = response.json['data']
        assert isinstance(data['user'], object)
        assert data['user']['first_name'] == 'John'
        assert data['user']['last_name'] == 'Smith'
        assert data['user']['custom_attributes']['bio'] == 'EasyMart'
        assert data['user']['custom_attributes']['gender'] == 'female'
        assert data['user']['phone'] == proccess_phone_number(phone)
        assert data['user']['is_vendor'] is False
        assert data['user']['is_tokenagent'] is False
        assert data['user']['is_groupaccount'] is False
        assert data['user']['location'] == 'Elwood'
        assert data['user']['business_usage_id'] == init_database.session.query(TransferUsage)\
            .filter_by(name=business_usage_name).first().id
        assert data['user']['referred_by'] == user_phone_accessor(create_transfer_account_user)

        if initial_disbursement is not None:
            assert data['user']['transfer_accounts'][0]['balance'] == initial_disbursement
        else:
            db_user = init_database.session.query(User).get(data['user']['id'])
            assert data['user']['transfer_accounts'][0]['balance'] == db_user.default_organisation.default_disbursement

        # Checks that we're calling the gps location fetching job, and passing the right data to it
        fn_inputs = mock_async_set_user_gps_from_location

        if gps_location:
            assert data['user']['lat'] == 12.02
            assert data['user']['lng'] == -15.04
            assert len(fn_inputs) == 0

        else:
            args, kwargs = fn_inputs[-1]
            assert kwargs == {'user_id': data['user']['id'], 'location': 'Elwood'}