def test_create_user():
    org_name2 = 'eorg'

    org = Organization.query.filter_by(full_name=org_name2).one()
    ciso_role = MembershipRole.query.filter_by(name='CISO').one()

    user_dict = {
        'name': 'testi 123',
        'email': '*****@*****.**',
        'password': '******',
        'birthdate': '1999-09-09',
    }

    (user, message) = User.create(user_dict)
    db.session.commit()
    assert user.id, 'User id set'
    assert message == 'User added', 'correct message'

    (user_alias, message) = User.create(user_dict)
    db.session.commit()
    assert user_alias.id, 'User id set'
    assert user_alias.alias_user_id, 'User alias id set'
    assert message == 'User aliased', 'correct message'

    organization_membership_dict = {
        'email': '*****@*****.**',
        'phone': '+43234234234',
        'membership_role_id': ciso_role.id,
        'organization_id': org.id,
        'user_id': user.id,
    }

    (organization_membership, message) = \
          OrganizationMembership.upsert(organization_membership_dict)
    db.session.commit()

    assert organization_membership.user_id == user.id, 'correct user set'
    assert organization_membership.user.name == 'testi 123', 'user name set'
    assert organization_membership.organization.full_name == org_name2, 'user name set'
    assert organization_membership.membership_role_id == ciso_role.id, 'role_id set'

    # create OrgAdmin
    admin_role = MembershipRole.query.filter_by(name='OrgAdmin').one()

    organization_membership_dict = {
        'email': '*****@*****.**',
        'phone': '+43234234234',
        'membership_role_id': admin_role.id,
        'organization_id': org.id,
        'user_id': user.id,
    }

    (organization_membership, message) = \
          OrganizationMembership.upsert(organization_membership_dict)
    db.session.commit()
    assert organization_membership.membership_role.name == 'OrgAdmin', 'role_id set'

    with pytest.raises(AttributeError):
        (organization_membership, message) = \
              OrganizationMembership.upsert(organization_membership_dict)
def update_cp_organization_membership(membership_id):
    """Update organization membership details"""

    existing_membership = OrganizationMembership.query.filter(
        OrganizationMembership.id == membership_id).first()

    if not existing_membership:
        return redirect(url_for('cp.add_cp_organization_membership'))

    check_membership_permissions(existing_membership)

    try:
        (membership,
         message) = OrganizationMembership.upsert(request.json,
                                                  existing_membership)
        check_membership_permissions(membership)
    except AttributeError as ae:
        db.session.rollback()
        message = 'Attribute error. Invalid email, phone or mobile? ' + str(ae)
        return ApiResponse({
            'message': message,
        }, 422, {})
    except Exception as ae:
        db.session.rollback()
        message = "something went wrong, please contact admin: " + str(ae)
        return ApiResponse({
            'message': message,
        }, 418, {})

    db.session.commit()
    return ApiResponse({'message': message})
def add_cp_organization_membership():
    """Add new organization membership

    :>json string message: Status message
    :>json integer id: Organization membership ID

    :status 200: Organization membership details were successfully saved

    :status 400: Bad request
    :status 401: Authorization failure. The client MAY repeat the request with
        a suitable API-Authorization header field. If the request already
        included Authorization credentials, then the 401 response indicates

        that authorization has been refused for those credentials.
    :status 403: Access denied. Authorization will not help and the request
        SHOULD NOT be repeated.
    :status 422: Validation error
    """
    try:
        (membership, message) = OrganizationMembership.upsert(request.json)
        check_membership_permissions(membership)
    except AttributeError as ae:
        message = 'Attribute error. Invalid email, phone or mobile?'
        return ApiResponse({
            'message': message,
        }, 422, {})
    except Exception as ae:
        message = "something went wrong, please contact admin: " + str(ae)
        return ApiResponse({
            'message': message,
        }, 418, {})

    db.session.commit()
    return  ApiResponse({'organization_membership': membership.serialize(),
            'message': message}, 201, \
           {'Location': url_for('cp.get_cp_organization_membership',
                                membership_id=membership.id)})