def setup_module(): cfg.dse_keyspace = TEST_KEYSPACE initialise() connect() create_tables() create_default_users() create_root() grp1 = Group.create(name=GRP1_NAME) grp2 = Group.create(name=GRP2_NAME) grp3 = Group.create(name=GRP3_NAME) user_name = USR1_NAME email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True groups = [GRP1_NAME] user1 = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) user_name = USR2_NAME email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = False groups = [GRP1_NAME, GRP2_NAME] user2 = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups)
def test_users(): user_name = uuid.uuid4().hex email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True groups = ['grp1'] notification_username = uuid.uuid4().hex # Simple create user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) user = User.find(user_name) assert user.get_groups() == groups assert user.is_active() == True assert user.is_authenticated() == True with pytest.raises(UserConflictError): user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) user.delete() # Create with a username for notification user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups, username=notification_username) user.delete()
def test_authenticate(mocker): user_name = uuid.uuid4().hex email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True groups = ['grp1'] user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) user = User.find(user_name) assert user.authenticate(password) assert not user.authenticate(uuid.uuid4().hex) # Check ldap authentication (mock the actual test) cfg.auth_ldap_server_uri = "ldap://ldap.example.com" cfg.auth_ldap_user_dn_template = "uid=%(user)s,ou=users,dc=example,dc=com" mocker.patch('ldap.ldapobject.SimpleLDAPObject.simple_bind_s', return_value=True) user.update(ldap=True) assert user.authenticate(password) # Check inactive user user.update(active=False) assert not user.authenticate(password)
def new_user(request): """Create a new user""" if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): data = form.cleaned_data user = User.create( name=data.get("username"), password=data.get("password").encode("ascii", "ignore"), email=data.get("email", ""), administrator=data.get("administrator", False), active=data.get("active", False), username=request.user.name, ) messages.add_message( request, messages.INFO, "The user '{}' has been created".format(user.name), ) return redirect("users:home") else: form = UserForm() ctx = { "form": form, } return render(request, "users/new.html", ctx)
def create_user(request): """Expecting json in the body: { "username": username, "password": password, "email": email, "administrator": is_admin } """ try: body = request.body request_body = json.loads(body) except (TypeError, json.JSONDecodeError): return Response("Invalid JSON body", status=HTTP_400_BAD_REQUEST) try: username = request_body["username"] except KeyError: return Response("Missing username", status=HTTP_400_BAD_REQUEST) user_db = User.find(username) if user_db: return Response("User already exists", status=HTTP_409_CONFLICT) try: email = request_body["email"] except KeyError: return Response("Missing email", status=HTTP_400_BAD_REQUEST) try: password = request_body["password"] except KeyError: return Response("Missing password", status=HTTP_400_BAD_REQUEST) administrator = request_body.get("administrator", False) new_user_db = User.create( name=username, password=password, email=email, administrator=administrator ) return Response(new_user_db.to_dict(), status=HTTP_201_CREATED)
def mk_ldap_user(self, args): """Create a new ldap user. Ask in the terminal for mandatory fields""" if not args[ARG_NAME]: name = input("Please enter the user's username: "******"Username {} already exists".format(name)) return admin = input("Is this an administrator? [y/N] ") pwd = random_password(20) User.create( name=name, password=pwd, email="STORED_IN_LDAP", ldap=True, administrator=(admin.lower() in ["true", "y", "yes"]), ) print(MSG_USER_CREATED.format(name))
def create_random_user(groups): user_name = uuid.uuid4().hex email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True return User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups)
def create_default_users(): """Create some users and groups Users and groups are defined in DEFAULT_GROUPS and DEFAULT_USERS in the :mod:`radon.model.config` module, . """ for name in cfg.default_groups: try: Group.create(name=name) except GroupConflictError: pass for name, email, pwd, is_admin, groups in cfg.default_users: try: User.create(name=name, email=email, password=pwd, administrator=is_admin, groups=groups) except UserConflictError: pass
def setup_module(): cfg.dse_keyspace = TEST_KEYSPACE initialise() connect() create_tables() create_default_users() create_root() pwd = uuid.uuid4().hex email = uuid.uuid4().hex grp1 = Group.create(name="grp1") u1 = User.create(name="user1", password=pwd, email=email, administrator=True) u2 = User.create(name="user2", password=pwd, email=email, administrator=False) grp1.add_users(["user2"]) try: coll = Collection.create("/", "1") coll = Collection.create("/1", "11") coll = Collection.create("/1", "12") coll = Collection.create("/1", "13") coll = Collection.create("/", "2") coll = Collection.create("/2", "21") coll = Collection.create("/2/21", "211") coll = Collection.create("/2/21", "212") r = Resource.create("/1/11", "a") r = Resource.create("/1/11", "b", url=TEST_URL) r = Resource.create("/1/12", "c") r = Resource.create("/1/13", "d") r = Resource.create("/2/21/211", "e") r = Resource.create("/2/21/212", "f") r = Resource.create("/", "g") except: # If collections or resources already exist pass
def mk_user(self, args): """Create a new user. Ask in the terminal for mandatory fields""" if not args[ARG_NAME]: name = input("Please enter the user's username: "******"Username {} already exists".format(name)) return admin = input("Is this an administrator? [y/N] ") email = "" while not email: email = input("Please enter the user's email address: ") pwd = "" while not pwd: pwd = getpass("Please enter the user's password: "******"true", "y", "yes"]), ) print(MSG_USER_CREATED.format(name))
def test_update(): user_name = uuid.uuid4().hex email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True groups = ['grp1'] notification_username = uuid.uuid4().hex # Simple create user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) new_password = uuid.uuid4().hex new_email = uuid.uuid4().hex user.update(password=new_password) user.update(email=new_email, username=notification_username) user = User.find(user_name)
def test_dict(): user_name = uuid.uuid4().hex email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True groups = ['grp1'] notification_username = uuid.uuid4().hex # Simple create user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) user = User.find(user_name) user_dict = user.to_dict() assert user_dict['uuid'] == user.uuid assert user_dict['administrator'] == True user.delete()
def test_group(): grp1_name = uuid.uuid4().hex grp2_name = uuid.uuid4().hex grp1 = Group.create(name=grp1_name) grp2 = Group.create(name=grp2_name) user_name = uuid.uuid4().hex email = uuid.uuid4().hex password = uuid.uuid4().hex administrator = True groups = [grp1_name, grp2_name] user = User.create(name=user_name, email=email, password=password, administrator=administrator, groups=groups) assert set(user.get_groups()) == set([grp1_name, grp2_name]) user.rm_group(grp1_name) assert set(user.get_groups()) == set([grp2_name])