Beispiel #1
0
def register():
    form = RegisterForm()

    if form.validate_on_submit():
        logout_session_clear()
        user = User(email=form.email.data)

        user.hash_password(form.password.data)
        user.activity.created = utc_now()
        verification_code = uuid.uuid4()

        user.activity.payment_reference = user.uid

        user.activity.email_verification = EmailVerification(
            verification_code=str(verification_code))
        user.activity.activity_audit.append(
            ActivityAudit(uid=user.uid,
                          created=utc_now(),
                          code="USER CREATED",
                          text="User created",
                          headers={
                              "REMOTE_ADDR": request.environ["REMOTE_ADDR"],
                              "HTTP_USER_AGENT":
                              request.environ["HTTP_USER_AGENT"],
                              "REMOTE_PORT": request.environ["REMOTE_PORT"],
                          }).save())

        user.save()

        user_registered_email(user.email, user.uid, verification_code)

        return render_template('auth/user_registered.html')
    return render_template('auth/register.html', form=form)
Beispiel #2
0
def make_user():
    email = request.json.get('email')
    password = request.json.get('password')
    if email is None or password is None:
        response = jsonify({'status': 'Fail', 'message': 'Missing parameters'})
        response.status_code = 400
        return response

    if User.objects.filter(email=email).first() is not None:
        response = jsonify({'status': 'Fail', 'message': 'User already exists'})
        response.status_code = 400
        return response

    user = User(email=email)
    user.hash_password(password)
    user.activity.created = now()
    verification_code = uuid.uuid4()
    user.activity.verification_code = str(verification_code)
    user.save()

    user_registered_email(email, verification_code)

    response = jsonify({'status': 'Success', 'message': 'User created'})
    response.status_code = 201
    return response
Beispiel #3
0
    def test_new_dataset_is_searchable(self):
        '''
        Tests that a newly added dataset is properly indexed and searchable.

        Procedure:
            1) Start with an empty database.
            2) Assert that a search for 'dolphin' returns nothing.
            3) Create a new dataset with name='dolphin' and commit to db.
            4) Assert that 'dolphin' search returns the dataset just created.
        '''
        query, ___ = Dataset.search('dolphin')
        self.assertEqual(len(query.all()), 0)

        # Initialize an owner for the dataset
        user = User(username='******', password_hash=User.hash_password('test'))
        db.session.add(user)
        db.session.commit()

        # Create dataset and add it
        new = Dataset(citation='',
                      description='',
                      name='dolphin',
                      url='https://github.com/cvfadmin/COVE',
                      owner_id=1)
        db.session.add(new)
        db.session.commit()

        self.app.elasticsearch.indices.refresh(index='datasets')
        query, ___ = Dataset.search('dolphin')
        self.assertEqual(len(query.all()), 1)
Beispiel #4
0
    def test_dataset_basic_text_search(self):
        '''
        Tests that a database can do full-text search for datasets.

        Test 1: Basic exact-text search example works.
        Test 2: Datasets with matches in any field (name, description, citation)
                of the dataset
        Test 3: Datasets with matches in the 'name' field have higher relevance.
        '''
        # Initialize an owner for the dataset
        user = User(username='******', password_hash=User.hash_password('test'))
        db.session.add(user)
        db.session.commit()

        # Create 3 datasets and commit them
        d1 = Dataset(
            id=1,
            citation='Australia',
            description='Pictures of adult kangaroos in their habitat.',
            name='Video of Jumping Kangaroos',
            url='https://github.com/cvfadmin/COVE',
            owner_id=1)
        d2 = Dataset(
            id=2,
            citation='Eric Jiang',
            description='Video of a baby rabbit playing around the house.',
            name='A white rabbit hops onto the table.',
            url='https://github.com/cvfadmin/COVE',
            owner_id=1)
        d3 = Dataset(id=3,
                     citation='A gym',
                     description='An ad about why jumping jacks are healthy',
                     name='Jumping jacks is your friend.',
                     url='https://github.com/cvfadmin/COVE',
                     owner_id=1)
        db.session.add_all([d1, d2, d3])
        db.session.commit()

        # Some basic searching
        self.app.elasticsearch.indices.refresh(index='datasets')
        query, __ = Dataset.search('jacks')
        query2, __ = Dataset.search('A white rabbit hops onto the table')
        query3, __ = Dataset.search('Jumping')
        self.assertEqual(query.first().id, 3)
        self.assertEqual(query2.first().id, 2)
        self.assertEqual(len(query3.all()), 2)

        # Search different fields
        query4, __ = Dataset.search('habitat')  # description field
        query5, __ = Dataset.search('Eric Jiang')  # citation field
        self.assertEqual(query4.first().id, 1)
        self.assertEqual(query5.first().id, 2)

        # Matches in name have higher priority
        query6, __ = Dataset.search('Video')
        self.assertEqual(len(query6.all()), 2)
        self.assertEqual(query6.all()[0].id, 1)
        self.assertEqual(query6.all()[1].id, 2)
Beispiel #5
0
 def test_password_hashing(self):
     '''Tests that the user's password can be hashed verified.'''
     username = '******'
     password = '******'
     wrong_password = '******'
     user = User(
         username=username,
         password_hash=User.hash_password(password),
     )
     self.assertTrue(user.verify_password(password))
     self.assertFalse(user.verify_password(wrong_password))
Beispiel #6
0
    def test_updated_dataset_is_searchable(self):
        '''
        Tests that an updated dataset is properly indexed and searchable.

        Procedure 1: New additions can be searched for.
            1) Create and add a dataset with name='crackers' to the db.
            2) Assert that a search for 'soup' returns nothing.
            3) Update the name of the dataset to name='crackers and soup'.
            4) Assert that a search for 'soup' returns the dataset.

        Procedure 2: Deletions are no longer found when searched for.
            1) Use dataset from procedure 1.
            2) Update the name of the dataset to name='soup'
            3) Assert that a search for 'crackers' returns nothing.
        '''

        # -------------------------- Procedure 1 -----------------------------
        # Initialize an owner for the dataset
        user = User(username='******', password_hash=User.hash_password('test'))
        db.session.add(user)
        db.session.commit()

        # Create dataset and add it
        new = Dataset(citation='',
                      description='',
                      name='Crackers',
                      url='https://github.com/cvfadmin/COVE',
                      owner_id=1)
        db.session.add(new)
        db.session.commit()

        # Assert 'soup' search returns nothing.
        self.app.elasticsearch.indices.refresh(index='datasets')
        query, __ = Dataset.search('soup')
        self.assertEqual(len(query.all()), 0)

        # 'name' now contains 'and' and 'soup'.
        # Search for either should yield the dataset.
        new.name = 'crackers and soup'
        db.session.commit()
        self.app.elasticsearch.indices.refresh(index='datasets')
        query, __ = Dataset.search('soup')
        self.assertEqual(len(query.all()), 1)

        # -------------------------- Procedure 2 -----------------------------
        # 'name' no longer contains 'crackers'. Search should return none.
        new.name = 'soup'
        db.session.commit()
        self.app.elasticsearch.indices.refresh(index='datasets')
        query, __ = Dataset.search('crackers')
        self.assertEqual(len(query.all()), 0)
Beispiel #7
0
    def test_deleted_dataset_is_not_searchable(self):
        '''
        Tests that a deleted dataset is no longer searchable.

        Procedure:
            1) Create and add a dataset name='racket'
            2) Assert that searching 'racket' returns the dataset.
            3) Delete the dataset.
            4) Assert that a search for 'racket' returns None.
        '''
        # Initialize an owner for the dataset
        user = User(username='******', password_hash=User.hash_password('test'))
        db.session.add(user)
        db.session.commit()

        # Create dataset and add it
        new = Dataset(citation='',
                      description='',
                      name='racket',
                      url='https://github.com/cvfadmin/COVE',
                      owner_id=1)
        db.session.add(new)
        db.session.commit()

        # Assert 'racket' search returns dataset.
        self.app.elasticsearch.indices.refresh(index='datasets')
        query, __ = Dataset.search('racket')
        self.assertEqual(len(query.all()), 1)

        db.session.delete(new)
        db.session.commit()

        # Assert 'racket' search returns dataset.
        self.app.elasticsearch.indices.refresh(index='datasets')
        query, __ = Dataset.search('racket')
        self.assertEqual(len(query.all()), 0)
Beispiel #8
0
 def generate_db_elements(self):
     admin = User(login=self.admin_login, password=unicode(User.hash_password(self.admin_password)), admin=True)
     invite = Invite(invite=self.invite_str, email=self.invite_email)
     db.session.add_all([admin, invite])
     db.session.commit()
Beispiel #9
0
    def test_dataset_text_search_english_analyzer(self):
        '''
        Tests that our search functionality can successfully intepret
        english to an extent.

        Test 1: Uppercase and lowercase are ignore.
        Test 2: Search treats singular or plural forms the similarly.
        Test 3: Search ignores apostrophes ('s').
        Test 4: Search ignores tense.
        '''
        # Initialize an owner for the dataset
        user = User(username='******', password_hash=User.hash_password('test'))
        db.session.add(user)
        db.session.commit()

        # Create 3 datasets and commit them
        d1 = Dataset(
            id=1,
            citation='Australia',
            description='Pictures of adult kangaroos in their habitat.',
            name='Video of Jumping Kangaroos',
            url='https://github.com/cvfadmin/COVE',
            owner_id=1)
        d2 = Dataset(
            id=2,
            citation='Eric Jiang',
            description='Video of a baby rabbit playing around the house.',
            name='A white rabbit hops onto the table.',
            url='https://github.com/cvfadmin/COVE',
            owner_id=1)
        d3 = Dataset(id=3,
                     citation='A gym',
                     description='An ad about why jumping jacks are healthy',
                     name='Jumping jacks is your friend.',
                     url='https://github.com/cvfadmin/COVE',
                     owner_id=1)
        db.session.add_all([d1, d2, d3])
        db.session.commit()

        # Refresh index before search
        self.app.elasticsearch.indices.refresh(index='datasets')

        query, __ = Dataset.search('KaNgAroos')
        query2, __ = Dataset.search('jack')
        query3, __ = Dataset.search('rabbits')
        query4, __ = Dataset.search("Eric's")
        query5, __ = Dataset.search('jumped')
        query6, __ = Dataset.search('babies')

        # Test letter cases
        self.assertEqual(query.first().id, 1)

        # Test plural and singular forms
        self.assertEqual(query2.first().id, 3)
        self.assertEqual(query3.first().id, 2)

        # Test 's
        self.assertEqual(query4.first().id, 2)

        # Test tense
        self.assertEqual(len(query5.all()), 2)
        self.assertEqual(query6.first().id, 2)