Esempio n. 1
0
 def test_updated_datetime_attributes(self):
     """
     tests that the datetime attribute updated_at changes
     when the save() method is implemented
     """
     first_time = datetime.now()
     new_obj = Profile()
     second_time = datetime.now()
     # tests if object's created_at time is between timestamps
     self.assertTrue(first_time <= new_obj.created_at <= second_time)
     # tests if object's updated_at is within the same timestamps
     self.assertTrue(first_time <= new_obj.updated_at <= second_time)
     # gets timestamps of current attributes and pauses a moment
     original_created_at = new_obj.created_at
     original_updated_at = new_obj.updated_at
     sleep(1)
     # adds required attributes so the object can be saved; saves object
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     new_obj.save()
     # tests that the object's updated_at has changed and is later
     self.assertNotEqual(original_updated_at, new_obj.updated_at)
     self.assertTrue(original_updated_at < new_obj.updated_at)
     # tests that only the object's updated_at datetime has changed
     self.assertEqual(original_created_at, new_obj.created_at)
Esempio n. 2
0
def register():
    if request.method == 'GET':
        return redirect('/')
    logging.debug('add profile form : %s', str(request.form))
    logging.info('receive socket from /register-data -> profile: %s',
                 request.form['profile-name'])
    form = request.form
    profile = Profile()
    profile.name = form['profile-name']
    profile.firstname = form['profile-firstname']
    profile.address = form['profile-address']
    profile.comp_address = form['profile-comp_address']
    profile.city = form['profile-city']
    profile.zipcode = form['profile-zipcode']
    profile.country = form['profile-country']
    profile.phone = form['profile-phone']
    profile.email = form['profile-email']
    profile.siret = form['profile-siret']
    pdao = ProfileDAO()
    if pdao.insert(profile):
        logging.info('add profile %s OK', profile.name)
        session['logged_in'] = pdao.field(pdao.where('email', profile.email),
                                          'id')[0][0]
    else:
        logging.info('add profile %s FAILED', profile.name)
    return redirect('/')
Esempio n. 3
0
def register():
    logging.debug('add profile form : %s', str(request.form))
    logging.info('receive socket from /register-data -> profile: %s', request.form['profile-name'])
    form = request.form
    if form['profile-password'] != form['profile-repassword']:
        return render_template('v3-login.html', error=_('Your confirmation password does not match the password you entered'))
    profile = Profile()
    profile.name = form['profile-name']
    profile.firstname = form['profile-firstname']
    profile.address = form['profile-address']
    profile.comp_address = form['profile-comp_address']
    profile.city = form['profile-city']
    profile.zipcode = form['profile-zipcode']
    profile.country = form['profile-country']
    profile.phone = form['profile-phone']
    profile.email = form['profile-email']
    profile.siret = form['profile-siret']
    profile.password = form['profile-password']
    pdao = ProfileDAO()
    if pdao.insert(profile):
        logging.info('add profile %s OK', profile.name)
        session['logged_in'] = pdao.field(pdao.where('email', profile.email), 'id')[0][0]
    else:
        logging.info('add profile %s FAILED', profile.name)
        return render_template('v3-login.html', error=_('Impossible to create new user, please contact an admin !'))

    return redirect('/')
Esempio n. 4
0
    def test_init_method(self):
        """
        tests the __init__ method for instantiating new objects
        both new and from kwargs
        __init__ method calls on inherited BaseModel with super()
        """
        # creates new instance of Profile
        new_obj1 = Profile()
        # tests that the new object is of type Profile
        self.assertIs(type(new_obj1), Profile)
        # adds all attributes for testing
        # (id should be set by primary key)
        # (created_at, updated_at should be set by datetime)
        new_obj1.name = "test_name"
        new_obj1.email = "*****@*****.**"
        new_obj1.password = "******"
        new_obj1.company_school_name = "123"
        new_obj1.about_me = "This is for testing purposes"
        new_obj1.linkedin = "https://www.linkedin.com/in/test"
        new_obj1.social_media = "Social media links would go here"
        # attributes_dict sets up dictionary of attribute names and types
        attributes_dict = {
            "id": str,
            "created_at": datetime,
            "updated_at": datetime,
            "name": str,
            "email": str,
            "password": str,
            "company_school_name": str,
            "about_me": str,
            "linkedin": str,
            "social_media": str
        }
        # loops through attributes_dict as subTests to check each attribute
        for attr, attr_type in attributes_dict.items():
            with self.subTest(attr=attr, attr_type=attr_type):
                # tests the expected attribute is in the object's dict
                self.assertIn(attr, new_obj1.__dict__)
                # tests the attribute is the expected type
                self.assertIs(type(new_obj1.__dict__[attr]), attr_type)
        # sets kwargs using object's dict and uses to create new object
        kwargs = new_obj1.__dict__
        new_obj2 = Profile(**kwargs)
        # tests that the new object is of type Profile
        self.assertIs(type(new_obj2), Profile)
        # loops through attributes_dict as subTests to check each attribute
        for attr, attr_type in attributes_dict.items():
            with self.subTest(attr=attr, attr_type=attr_type):
                # tests the expected attribute is in the object's dict
                self.assertIn(attr, new_obj2.__dict__)
                # tests the attribute is the expected type
                self.assertIs(type(new_obj2.__dict__[attr]), attr_type)
                # tests the value of name attribute matches the original object
                self.assertEqual(new_obj1.__dict__[attr],
                                 new_obj2.__dict__[attr])

        # tests that __class__ is not set in object 2
        self.assertNotIn('__class__', new_obj2.__dict__)
Esempio n. 5
0
 def test_delete_profile(self):
     """
     tests the storage.delete() method removes and commits obj to database
     for an object from the Profile class
     """
     # connect to MySQL database through MySQLdb and get initial count
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     objs_for_count1 = cur.fetchall()
     # creates new instance of Profile
     new_obj = Profile()
     # tests that the new object is of type Profile
     self.assertIs(type(new_obj), Profile)
     # adds all attributes required for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     # save the object with BaseModel save method
     # save method calls storage.new() and storage.save()
     new_obj.save()
     # closes connection to database and restarts connection with MySQLdb
     cur.close()
     db.close()
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     objs_for_count2 = cur.fetchall()
     # tests that there is one more obj saved to profiles table in db
     self.assertEqual(len(objs_for_count1) + 1, len(objs_for_count2))
     # delete the object with BaseModel delete method
     # delete instance method calls storage.delete() and storage.save()
     new_obj.delete()
     # closes connection to database and restarts connection with MySQLdb
     cur.close()
     db.close()
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     objs_for_count3 = cur.fetchall()
     # tests that there is one less obj in profiles table in db
     self.assertEqual(len(objs_for_count2) - 1, len(objs_for_count3))
     self.assertEqual(len(objs_for_count1), len(objs_for_count3))
     # closes the connection
     cur.close()
     db.close()
Esempio n. 6
0
 def test_all_profiles_dict(self):
     """
     tests return of all method when class is Profile
     """
     # connect to MySQL database through MySQLdb and get initial count
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM profiles""")
     profile_objs = cur.fetchall()
     total_profile_count = len(profile_objs)
     # call storage.all() method
     all_profile_objs = storage.all(Profile)
     profile_count1 = len(all_profile_objs.keys())
     # tests that all method returns same count of Identity objects
     self.assertEqual(total_profile_count, profile_count1)
     # tests that all method returns dictionary
     self.assertIsInstance(all_profile_objs, dict)
     # creates new Profile obj to test with
     new_obj = Profile()
     # adds all attributes required for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     # saves new object to the database
     new_obj.save()
     # re-call storage.all() method and test that count increased by 1
     all_profile_objs = storage.all(Profile)
     profile_count2 = len(all_profile_objs.keys())
     self.assertEqual(profile_count1 + 1, profile_count2)
     # tests that newly created obj is in dictionary with correct key
     self.assertIsInstance(storage.all(), dict)
     dict_key = "{}.{}".format("Profile", new_obj.id)
     self.assertIn(dict_key, storage.all())
     # get obj attributes from stroage.all() dictionary using obj id
     # test that retrieved attributes match expected values
     obj_class = storage.all().get("Profile.{}".format(
         new_obj.id)).__class__.__name__
     self.assertEqual("Profile", obj_class)
     obj_name = storage.all().get("Profile.{}".format(new_obj.id)).name
     self.assertEqual("test_name", obj_name)
     obj_email = storage.all().get("Profile.{}".format(new_obj.id)).email
     self.assertEqual("*****@*****.**", obj_email)
     # delete new object from the database
     new_obj.delete()
     # re-call storage.all() method and test that count decreased by 1
     all_profile_objs = storage.all(Profile)
     profile_count3 = len(all_profile_objs.keys())
     self.assertEqual(profile_count2 - 1, profile_count3)
     self.assertEqual(profile_count1, profile_count3)
     # tests that new object is no longer in return dictionary
     self.assertNotIn(dict_key, storage.all())
Esempio n. 7
0
 def test_all_profiles_count(self):
     """
     tests all method retrieves all objects when class is Profile
     """
     # connect to MySQL database through MySQLdb and get initial count
     db = connect(host=ION_MYSQL_HOST,
                  user=ION_MYSQL_USER,
                  passwd=ION_MYSQL_PWD,
                  db=ION_MYSQL_DB)
     cur = db.cursor()
     cur.execute("""SELECT * FROM identities""")
     identity_objs = cur.fetchall()
     cur.execute("""SELECT * FROM profiles""")
     profile_objs = cur.fetchall()
     cur.execute("""SELECT * FROM skills""")
     skills_objs = cur.fetchall()
     total_count = len(identity_objs) + len(profile_objs) + len(skills_objs)
     total_profile_count = len(profile_objs)
     # call storage.all() method, both with and without class specified
     all_objs = storage.all()
     count1 = len(all_objs.keys())
     all_profile_objs = storage.all(Profile)
     profile_count1 = len(all_profile_objs.keys())
     # tests that counts from all method match current database
     self.assertEqual(total_count, count1)
     self.assertEqual(total_profile_count, profile_count1)
     # creates new Profile obj to test with
     new_obj = Profile()
     # adds all attributes required for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     # saves new object to the database
     new_obj.save()
     # re-call storage.all() method
     all_objs = storage.all()
     count2 = len(all_objs.keys())
     all_profile_objs = storage.all(Profile)
     profile_count2 = len(all_profile_objs.keys())
     # tests that counts increased by 1
     self.assertEqual(count1 + 1, count2)
     self.assertEqual(profile_count1 + 1, profile_count2)
     # deletes new object from the database
     new_obj.delete()
     # re-call storage.all() method
     all_objs = storage.all()
     count3 = len(all_objs.keys())
     all_profile_objs = storage.all(Profile)
     profile_count3 = len(all_profile_objs.keys())
     # tests that count decreased by 1
     self.assertEqual(count2 - 1, count3)
     self.assertEqual(count1, count3)
     self.assertEqual(profile_count2 - 1, profile_count3)
     self.assertEqual(profile_count1, profile_count3)
Esempio n. 8
0
 def test_save_method(self, mock_storage):
     """
     tests that the save() method inherited from BaseModel calls on
     storage.new() to add and commit the object to the database
     """
     # creates new instance of Profile
     new_obj = Profile()
     # adds name and email as required attribute for database
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     # saves new instance and tests if storage method new is called
     new_obj.save()
     self.assertTrue(mock_storage.new.called)
Esempio n. 9
0
 def test_attribute_types(self):
     """
     tests the class attributes exist and are of correct type
     also tests instantiation of new object
     """
     # creates new instance of Profile
     new_obj = Profile()
     # adds name and email as required attribute for database
     # adds optional attributes for testing
     # (id should be set by primary key)
     # (created_at, updated_at should be set by datetime)
     new_obj.name = "test_name"
     new_obj.email = "*****@*****.**"
     new_obj.password = "******"
     new_obj.company_school_name = "123"
     new_obj.about_me = "This is for testing purposes"
     new_obj.linkedin = "https://www.linkedin.com/in/test"
     new_obj.social_media = "Social media links would go here"
     # attributes_dict sets up dictionary of attribute names and types
     attributes_dict = {
         "id": str,
         "created_at": datetime,
         "updated_at": datetime,
         "name": str,
         "email": str,
         "password": str,
         "company_school_name": str,
         "about_me": str,
         "linkedin": str,
         "social_media": str
     }
     # loops through attributes_dict as subTests to check each attribute
     for attr, attr_type in attributes_dict.items():
         with self.subTest(attr=attr, attr_type=attr_type):
             # tests the expected attribute is in the instance's dict
             self.assertIn(attr, new_obj.__dict__)
             # tests the attribute is the expected type
             self.assertIs(type(new_obj.__dict__[attr]), attr_type)