Ejemplo n.º 1
0
def create_user(username, password, email, is_admin=False):

    new_user(username=username,
             password=password,
             email=email,
             description="",
             confirmed=True,
             is_admin=is_admin,
             is_remote=False,
             is_private=False)
Ejemplo n.º 2
0
 def get_or_create_remote_user(self):
     """ 
         Returns an instance of User after looking for it using it's ap_id
     """
     logging.debug(self.uri)
     user = UserProfile.get_or_none(ap_id=self.uri)
     if user == None:
         user = self.dereference()
         hostname = urlparse(user.id).hostname
         #username = "******".format(user.preferredUsername, hostname)
         logging.debug(
             f"I'm going to request the creation of user with username @{user.preferredUsername}"
         )
         user = new_user(username=user.preferredUsername,
                         name=user.name,
                         ap_id=user.id,
                         is_remote=True,
                         email=None,
                         password="******",
                         description=user.summary,
                         is_private=user.manuallyApprovesFollowers,
                         public_key=user.publicKey['publicKeyPem'])
     #print(user)
     logging.debug(f"remote user: {user}")
     return user
Ejemplo n.º 3
0
def new_user():
    username = request.json.get('username')
    password = request.json.get('password')

    c = check_user_fields(username, password)
    if c is not None:
        return c

    user = um.new_user(username, password)

    return jsonify({
        'id': user.id,
        'username': user.username,
        'role': USER_ROLES[find_user_role(user.role)]
    }), 201
Ejemplo n.º 4
0
def populate_for_travis():
    for i in range(15):
        target = new_user(username=f'test{i}',
                          password=passw,
                          email=f'fort{i}@gma.com',
                          confirmed=True,
                          is_admin=True,
                          is_remote=False,
                          is_private=False)
        target.follow(yab)

        #crate some followers
        for j in range(i):
            if j != i:
                user = User.get(username=f'test{j}').profile.get()
                user.follow(target, True)
Ejemplo n.º 5
0
    def test_register_user_username_exists(self):
        try:
            profile = new_user(username=user['username'],
                               password=user['password'],
                               email=user['email'])
        except:
            pass

        body = urllib.parse.urlencode({
            'username': user['username'],
            'password': user['password'],
            'password_confirmation': user['password'],
            'email': user['email']
        })

        response = self.fetch('/api/v1/register', method='POST', body=body)
        self.assertEqual(response.code, 400)
Ejemplo n.º 6
0
    async def post(self):
        username = self.get_argument('username')
        password = self.get_argument('password')
        confirmation = self.get_argument('password_confirmation')
        email = self.get_argument('email')
        
        valid_password = password == confirmation

        if '@' not in parseaddr(email)[1]: 
            self.set_status(500)
            self.write({"Error": "Invalid email"})
            self.finish()
            return

        # TODO: Move the logic from here to elsewhere
        username_count = await self.application.objects.count(User.select().where(User.username==username))

        free = username_count == 0
        log.debug(f'username is free: {free}')
        if valid_password and free:

            try:
                log.debug("Creating new user")
                profile = new_user(
                    username = username, 
                    password = password,
                    email = parseaddr(email)[1]
                )

                if not profile:
                    log.error("Error creating profile")
                    self.set_status(402)
                    self.write({"Error": "Wrong username. Valid characters are number, ascii letters and (.) (_)"})
                else:
                    self.set_status(200)
                    self.write(json.dumps(profile.to_json(), default=str))

            except Exception as e:
                log.error(e)
                self.set_status(500)
                self.write({"Error": "Error creating new user"})
        
        else:

            self.set_status(400)
            self.write({"Error": "User not available or password not matching"})
Ejemplo n.º 7
0
    async def post(self):

        username = self.get_argument('username')
        password = self.get_argument('password')
        confirmation = self.get_argument('password_confirmation')
        email = self.get_argument('email')

        valid_password = password == confirmation

        username_count = await self.application.objects.execute(
            User.select().where(User.username == username).count())

        free = username_count == 0
        logger.debug(f'username is free: {free}')
        if valid_password and free:

            try:

                logger.debug("Creating new user")
                profile = new_user(username=username,
                                   password=password,
                                   description="",
                                   email=parseaddr(email)[1])

                if not profile:
                    logger.error("Error creating profile")
                    self.set_status(402)
                    self.write({
                        "Error":
                        "Wrong username. Valid characters are number, ascii letters and (.) (_)"
                    })

            except Exception as e:
                logger.error(e)
                self.set_status(500)
                self.write({"Error": "Error creating new user"})

        else:

            self.set_status(400)
            self.write(
                {"Error": "User not available or password not matching"})
Ejemplo n.º 8
0
 def get_or_create_remote_user(self):
     """ 
         Returns an instance of User after looking for it using it's ap_id
     """
     user = UserProfile.get_or_none(ap_id=self.uri)
     if not user:
         user = self.dereference()
         hostname = urlparse(user.id).hostname
         username = "******".format(user.preferredUsername, hostname)
         user = new_user(
             username=user.preferredUsername,
             name=user.name,
             ap_id=user.id,
             is_remote=True,
             email = None,
             password = "******",
             description=user.summary,
             is_private=user.manuallyApprovesFollowers,
             public_key=user.publicKey['publicKeyPem']
         )
     #print(user)
     return user
Ejemplo n.º 9
0
    buff.write(b'--\r\n')
    headers = {'Content-Type': 'multipart/form-data; boundary=%s' % boundry}
    headers['Content-Length'] = str(buff.tell())
    return buff.getvalue(), headers


connect()
create_tables()

passw = "test"

#yab = UserProfile.create(username="******")
yab = new_user(username="******",
               password='******',
               email="*****@*****.**",
               description="A test user",
               confirmed=True,
               is_admin=True,
               is_remote=False,
               is_private=False)


def populate_db():
    for i in range(15):
        target = UserProfile.create(
            username=f'test{i}',
            password=passw,
            name=f'test#{i}',
            email=f'fort{i}@gma.com',
            confirmation_sent_at=datetime.datetime.now(),
            last_sign_in_at=1)