def test_get_followed_colles_by_user(self): test_user = User(name='Test_name', surname='Test surnames', email='*****@*****.**', password='') user_ctrl = get_user_ctrl(self.cnx) user_ctrl.insert(test_user) colla1 = Colla(name='Test colla 1', uni=0, color='#FFFFFF') colla2 = Colla(name='Test colla 2', uni=1, color='#FFFFFF') colla3 = Colla(name='Test colla 3', uni=1, color='#FFFFFF') colla_ctrl = get_colla_ctrl(self.cnx) colla_ctrl.insert(colla1) colla_ctrl.insert(colla2) colla_ctrl.insert(colla3) sql = "INSERT INTO follows(id_user, id_colla) VALUES ('%s','%s')" data = [(test_user.id, colla1.id), (test_user.id, colla2.id)] self.cnx.cursor().executemany(sql, data) following_ctrl = get_following_ctrl(self.cnx) following_colles = following_ctrl.get_followed_by_user(test_user.id) self.assertEquals(2, len(following_colles)) self.assertEquals(colla1, following_colles[0]) self.assertEquals(colla2, following_colles[1])
def create_token(email, user_id): m = hashlib.md5(email) token = m.hexdigest() db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) user_ctrl.add_token(user_id, token) return token
def log_in(): body = json.loads(request.data) email = body['email'] password = body['password'] db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) user = user_ctrl.get_by_email(email) if user is not None: if check_password(email, password): token = get_token_by_user_id(user_id=user.id) user.session_token = token user.admin = get_admin_ctrl( DB(db_configuration).get_database_connection()).is_admin( user.id) user.follows = follow_service.get_some_info_followed_colles_by_user( user.id) user.belongs = belong_service.get_some_info_belonging_colles_by_user( user.id) response = make_response( json.dumps(user, encoding='utf-8', cls=UserEncoder, indent=4), 200) response.headers[0] = ('Content-Type', 'application/json; charset=utf-8') return response return abort(403)
def get_wall(): user_id = request.args.get('user_id') noticies = [] if user_id is not None: db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl( DB(db_configuration).get_database_connection()) user = user_ctrl.get(user_id) if user: follow_ctrl = get_follow_ctrl( DB(db_configuration).get_database_connection()) colles_follow = follow_ctrl.get_name_followed_colles_by_user( user_id) belong_ctrl = get_belong_ctrl( DB(db_configuration).get_database_connection()) colles_belongs = belong_ctrl.get_name_belonging_colles_by_user( user_id) colles = colles_follow + colles_belongs noticies = busca_rss( "http://www.cccc.cat/continguts/rss/noticies-1", colles) else: abort(404) else: noticies = rss_info("http://www.cccc.cat/continguts/rss/noticies-1") return make_response(jsonify(noticies), 200)
def test_exists_by_email_return_false(self): sql = "INSERT INTO users (name, surnames, email, password) " \ "VALUES ('%s','%s','%s','%s')" % ('Test_Name', 'Test Surname', '*****@*****.**', 'tests password') cursor = self.cnx.cursor() cursor.execute(sql) exists = get_user_ctrl(self.cnx).exists_by_email('*****@*****.**') self.assertFalse(exists)
def get_user(user_id): db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) user = user_ctrl.get(user_id) if user is None: abort(404) else: user = user_service.get_all_info(user) return make_response(jsonify(user.__dict__), 200)
def test_create(self): created_user = User("Test_Name", "Test Surname", "*****@*****.**", "tests password") ctrl_user = get_user_ctrl(self.cnx) created_user = ctrl_user.insert(created_user) db_user = ctrl_user.get(created_user.id) self.assertIsNotNone(db_user) self.assertEquals("Test_Name", db_user.name) self.assertEquals("Test Surname", db_user.surname) self.assertEquals("*****@*****.**", db_user.email)
def add_attendant(practice_id): user_id = request.args.get('user_id') if user_id: practice = practice_service.get_practice(practice_id) from api.db.CtrlFactory import get_user_ctrl db_configuration = json.loads(open("api/db/db.json").read()) user = get_user_ctrl(DB(db_configuration).get_database_connection()).get(user_id) if practice and user: practice = practice_service.insert_attendant(user, practice) return make_response(jsonify(practice.__dict__), 201) else: abort(404) else: abort(400)
def create_user(name, surname, email, password, belonging_list, following_list): db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) user = User(name, surname, email, password=password) user = user_ctrl.insert(user) user.password = None belonging_ctrl = get_belonging_ctrl( DB(db_configuration).get_database_connection()) belonging_ctrl.insert_belonging_batch(belonging_list, user.id) following_ctrl = get_following_ctrl( DB(db_configuration).get_database_connection()) following_ctrl.insert_following_batch(following_list, user.id) return user
def test_get(self): sql = "INSERT INTO users (name, surnames, email, password) " \ "VALUES ('%s','%s','%s','%s')" % ('Test_Name', 'Test Surname', '*****@*****.**', 'tests password') cursor = self.cnx.cursor() cursor.execute(sql) last_id = cursor.lastrowid user = get_user_ctrl(self.cnx).get(last_id) self.assertIsNotNone(user) self.assertEquals(last_id, user.id) self.assertEquals('Test_Name', user.name) self.assertEquals('Test Surname', user.surname) self.assertEquals('*****@*****.**', user.email)
def revoke_admin(): user_id = request.args.get('user_id') colla_id = request.args.get('colla_id') if colla_id is None or user_id is None: abort(400) db_configuration = json.loads(open("api/db/db.json").read()) from api.db.CtrlFactory import get_user_ctrl user = get_user_ctrl(DB(db_configuration).get_database_connection()).get(user_id) from api.db.CtrlFactory import get_colla_ctrl colla = get_colla_ctrl(DB(db_configuration).get_database_connection()).get(colla_id) if colla is None or user is None: abort(404) from api.db.CtrlFactory import get_admin_ctrl get_admin_ctrl(DB(db_configuration).get_database_connection()).delete(user.id, colla.id) return make_response(jsonify({}), 202)
def get_all_info(user): db_configuration = json.loads(open("api/db/db.json").read()) bd_user = get_user_ctrl( DB(db_configuration).get_database_connection()).get(user.id) user = bd_user user.admin = get_admin_ctrl( DB(db_configuration).get_database_connection()).is_admin(user.id) user.session_token = get_token_ctrl( DB(db_configuration).get_database_connection()).get(user.id) user.belongs = get_belonging_ctrl( DB(db_configuration).get_database_connection() ).get_id_belonging_colles_by_user(user.id) user.follows = get_following_ctrl( DB(db_configuration).get_database_connection() ).get_id_followed_colles_by_user(user.id) return user
def update_user(user_id): db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) user = user_ctrl.get(user_id) if user is None: abort(404) else: body = json.loads(urllib.unquote(request.data)) new_password = body['password'] new_name = body['name'] new_surname = body['surname'] user.name = new_name user.surname = new_surname user.password = new_password user_ctrl.update(user) user = user_service.get_all_info(user) return make_response(jsonify(user.__dict__), 200)
def follow(user_id): db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) user = user_ctrl.get(user_id) if user is None: abort(404) else: colla_id = request.args.get('colla_id') if colla_id is not None: colla = get_colla_ctrl( DB(db_configuration).get_database_connection()).get(colla_id) if colla: user_service.add_following(user.id, colla_id) user = user_service.get_all_info(user) return make_response(jsonify(user.__dict__), 200) else: abort(404) abort(400)
def sign_in(): try: body = json.loads(urllib.unquote(request.data)) email = body['email'] password = body['password'] name = body['name'] surname = body['surname'] colles_that_belongs_to = body['belongs'] colles_followed = body['follows'] db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl( DB(db_configuration).get_database_connection()) new_user = user_ctrl.get_by_email(email) if new_user is None: new_user = create_user(name=name, surname=surname, email=email, password=password, belonging_list=colles_that_belongs_to, following_list=colles_followed) token = create_token(email, new_user.id) new_user.session_token = token new_user.follows = follow_service.get_some_info_followed_colles_by_user( new_user.id) new_user.belongs = belong_service.get_some_info_belonging_colles_by_user( new_user.id) new_user.admin = get_admin_ctrl( DB(db_configuration).get_database_connection()).is_admin( new_user.id) response = make_response( json.dumps(new_user, encoding='utf-8', cls=UserEncoder, indent=4), 201) response.headers[0] = ('Content-Type', 'application/json; charset=utf-8') return response abort(409) except KeyError: abort(500)
def check_password(email, password): db_configuration = json.loads(open("api/db/db.json").read()) user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection()) result = user_ctrl.check_password(email, password) return result
def test_get_but_doesnt_exist(self): user = get_user_ctrl(self.cnx).get(0) self.assertIsNone(user)