예제 #1
0
    def test_admin_token(self):
        """
        测试管理员token
        """
        u = User(
            email = '*****@*****.**',
            username = '******',
            password = b64encode('muxi304'),
            role_id = 2
        )
        db.session.add(u)
        db.session.commit()

        user_token_json = self.client.get(
            url_for('api.get_token'),
            headers = self.get_api_headers(
                '*****@*****.**', 'muxi304'
            )
        )
        user_token = eval(user_token_json.data).get('token')
        u = User.query.filter_by(email='*****@*****.**').first()
        expect_token = u.generate_auth_token()
        self.assertTrue(str(user_token) == str(expect_token))

        res = self.client.get(
            url_for('api.get_comments'),
            headers = self.get_token_headers(user_token)
        )
        self.assertTrue(res.status_code == 200)
예제 #2
0
    def test_tip_page(self):
        """
        测试首页: 管理员创建tip, 用户阅读tip
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        admin_token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_tip'),
                               headers=self.get_token_headers(admin_token),
                               data=json.dumps({
                                   'title': 'test tips',
                                   'img_url':
                                   'http://substack.net/images/rov.png',
                                   'body': 'this is a test tip body, enen',
                                   'author': 'test app'
                               }))
        self.assertTrue(res.status_code == 201)

        res = self.client.get(url_for('api.get_tip_id', id=1),
                              content_type='application/json')
        self.assertTrue(res.status_code == 200)
예제 #3
0
    def test_write_course_comment(self):
        """
        测试编写课程评论
        """
        u = User(
            email = '*****@*****.**',
            username = '******',
            password = b64encode('muxi304'),
            role_id = 2
        )
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
            headers = self.get_token_headers(token),
            data = json.dumps({
                'name': 'test course',
                'teacher': 'test teacher',
                'category_id': 1,
                'type_id': 1,
            })
        )

        res = self.client.post(url_for('api.new_comment', id=1),
            headers = self.get_token_headers(token),
            data = json.dumps({
                'body': 'this is a test comment',
                'tags': ''
            })
        )
        self.assertTrue(res.status_code == 201)
예제 #4
0
    def test_tip_page(self):
        """
        测试首页: 管理员创建tip, 用户阅读tip
        """
        u = User(
            email = '*****@*****.**',
            username = '******',
            password = b64encode('muxi304'),
            role_id = 2
        )
        db.session.add(u)
        db.session.commit()
        admin_token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_tip'),
            headers = self.get_token_headers(admin_token),
            data = json.dumps({
                'title': 'test tips',
                'img_url': 'http://substack.net/images/rov.png',
                'body': 'this is a test tip body, enen',
                'author': 'test app'
            })
        )
        self.assertTrue(res.status_code == 201)

        res = self.client.get(url_for('api.get_tip_id', id=1),
            content_type = 'application/json'
        )
        self.assertTrue(res.status_code == 200)
예제 #5
0
    def test_create_and_delete_question(self):
        """
        测试向特定课程提问和删除问题
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'name': 'test course',
                                   'teacher': 'test teacher',
                                   'category_id': 1,
                                   'type_id': 1,
                               }))

        res1 = self.client.post(
            url_for('api.new_question', id=1),
            headers=self.get_token_headers(token),
            data=json.dumps({'question_content': 'this is a test question'}))
        self.assertTrue(res1.status_code == 201)

        res2 = self.client.delete(url_for('api.delete_question', id=1),
                                  headers=self.get_token_headers(token),
                                  data=json.dumps({}))
        self.assertTrue(res2.status_code == 201)
예제 #6
0
    def test_write_course_comment(self):
        """
        测试编写课程评论
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'name': 'test course',
                                   'teacher': 'test teacher',
                                   'category_id': 1,
                                   'type_id': 1,
                               }))

        res = self.client.post(url_for('api.new_comment', id=1),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'body': 'this is a test comment',
                                   'tags': ''
                               }))
        self.assertTrue(res.status_code == 201)
예제 #7
0
    def test_like_comment(self):
        """
        测试给相应的评论点赞
        """
        u = User(
            email = '*****@*****.**',
            username = '******',
            password = b64encode('muxi304'),
            role_id = 2
        )
        db.session.add(u)
        db.session.commit()
        admin_token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
            headers = self.get_token_headers(admin_token),
            data = json.dumps({
                'name': 'test course',
                'teacher': 'test teacher',
                'category_id': 1,
                'type_id': 1,
            })
        )

        res = self.client.post(url_for('api.new_comment', id=1),
            headers = self.get_token_headers(admin_token),
            data = json.dumps({
                'body': 'this is a test comment',
                'tags': ''
            })
        )

        res = self.client.post(url_for('api.new_comments_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        likes = Comments.query.get_or_404(1).likes
        self.assertTrue(likes == 1)

        res = self.client.post(url_for('api.new_comments_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        self.assertTrue(res.status_code == 403)

        res = self.client.delete(url_for('api.new_comments_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        likes = Comments.query.get_or_404(1).likes
        self.assertTrue(likes == 0)

        res = self.client.delete(url_for('api.new_comments_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        self.assertTrue(res.status_code == 403)
예제 #8
0
    def test_normal_token(self):
        """
        测试普通用户token
        """
        u = User(email='*****@*****.**',
                 password=b64encode('muxi304'),
                 role_id=3)
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.get(url_for('api.get_comments'),
                              headers=self.get_token_headers(token))
        self.assertTrue(res.status_code == 403)
예제 #9
0
    def test_create_course(self):
        """
        测试管理员创建课程
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'name': 'test course',
                                   'teacher': 'test teacher',
                                   'category_id': 1,
                                   'type_id': 1,
                               }))
        self.assertTrue(res.status_code == 201)

        # test get course
        res = self.client.get(url_for("api.get_courses"))
        self.assertTrue(res.status_code == 200)

        res = self.client.get(url_for("api.get_courses", gg_cat=1,
                                      sort='view'))
        self.assertTrue(res.status_code == 200)

        # test get course by id
        res = self.client.get(url_for("api.get_course_id", id=5))
        self.assertTrue(res.status_code == 404)

        res = self.client.get(url_for("api.get_course_id", id=1))
        self.assertTrue(res.status_code == 200)

        # test admin update a course
        res = self.client.put(url_for("api.put_course", id=1),
                              headers=self.get_token_headers(token),
                              data=json.dumps({
                                  "name": "new test course name",
                                  "available": False
                              }))
        self.assertTrue(res.status_code == 200)
        updated_course = Courses.query.get_or_404(1)
        self.assertTrue(updated_course.name == "new test course name")
        self.assertTrue(updated_course.available == False)
예제 #10
0
    def test_like_tip(self):
        """
        测试给相应tip点赞
        """
        u = User(
            email = '*****@*****.**',
            username = '******',
            password = b64encode('muxi304'),
            role_id = 2
        )
        db.session.add(u)
        db.session.commit()
        admin_token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_tip'),
            headers = self.get_token_headers(admin_token),
            data = json.dumps({
                'title': 'test tips',
                'img_url': 'http://substack.net/images/rov.png',
                'body': 'this is a test tip body, enen',
                'author': 'test app'
            })
        )

        res = self.client.post(url_for('api.new_tips_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        likes = Tips.query.get_or_404(1).likes
        self.assertTrue(likes == 1)

        res = self.client.post(url_for('api.new_tips_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        self.assertTrue(res.status_code == 403)

        res = self.client.delete(url_for('api.new_tips_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        likes = Tips.query.get_or_404(1).likes
        self.assertTrue(likes == 0)

        res = self.client.delete(url_for('api.new_tips_id_like', id=1),
            headers = self.get_token_headers(admin_token)
        )
        self.assertTrue(res.status_code == 403)
예제 #11
0
    def test_like_comment(self):
        """
        测试给相应的评论点赞
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        admin_token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
                               headers=self.get_token_headers(admin_token),
                               data=json.dumps({
                                   'name': 'test course',
                                   'teacher': 'test teacher',
                                   'category_id': 1,
                                   'type_id': 1,
                               }))

        res = self.client.post(url_for('api.new_comment', id=1),
                               headers=self.get_token_headers(admin_token),
                               data=json.dumps({
                                   'body': 'this is a test comment',
                                   'tags': ''
                               }))

        res = self.client.post(url_for('api.new_comments_id_like', id=1),
                               headers=self.get_token_headers(admin_token))
        likes = Comments.query.get_or_404(1).likes
        self.assertTrue(likes == 1)

        res = self.client.post(url_for('api.new_comments_id_like', id=1),
                               headers=self.get_token_headers(admin_token))
        self.assertTrue(res.status_code == 403)

        res = self.client.delete(url_for('api.new_comments_id_like', id=1),
                                 headers=self.get_token_headers(admin_token))
        likes = Comments.query.get_or_404(1).likes
        self.assertTrue(likes == 0)

        res = self.client.delete(url_for('api.new_comments_id_like', id=1),
                                 headers=self.get_token_headers(admin_token))
        self.assertTrue(res.status_code == 403)
예제 #12
0
    def test_normal_token(self):
        """
        测试普通用户token
        """
        u = User(
            email = '*****@*****.**',
            password = b64encode('muxi304'),
            role_id = 3
        )
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.get(
            url_for('api.get_comments'),
            headers = self.get_token_headers(token)
        )
        self.assertTrue(res.status_code == 403)
예제 #13
0
 def decorated(*args, **kwargs):
     token_header = request.headers.get('authorization', None)
     if token_header:
         token_hash = token_header[6:]
         token_8 = base64.b64decode(token_hash)
         token = token_8[:-1]
         g.current_user = User.verify_auth_token(token)
         if not g.current_user.is_administrator():
             abort(403)
         return f(*args, **kwargs)
예제 #14
0
 def decorated(*args, **kwargs):
     token_header = request.headers.get('authorization', None)
     if token_header:
         token_hash = token_header[6:]
         token_8 = base64.b64decode(token_hash)
         token = token_8[:-1]
         g.current_user = User.verify_auth_token(token)
         if not g.current_user.is_administrator():
             abort(403)
         return f(*args, **kwargs)
예제 #15
0
def adduser():
    name = raw_input('Username> ')
    email = raw_input('Email> ')
    input_password = getpass('Password> ')
    role = input('Role(2:admin, 3:user)> ')

    password = base64.b64encode(input_password)
    new = User(name=name, email=email, password=password, role=role)
    db.session.add(new)
    db.session.commit()
    print "new user <{name}> created".format(name)
예제 #16
0
 def decorated(*args, **kwargs):
     token_header = request.headers.get('authorization', None)
     if token_header:
         token_hash = token_header[6:]
         token_8 = base64.b64decode(token_hash)
         token = token_8[:-1]
         g.current_user = User.verify_auth_token(token)
         if not g.current_user.can(Permission.MODERATE_COMMENTS):
             abort(403)
         return f(*args, **kwargs)
     else:
         abort(401)
예제 #17
0
    def test_admin_token(self):
        """
        测试管理员token
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()

        user_token_json = self.client.get(url_for('api.get_token'),
                                          headers=self.get_api_headers(
                                              '*****@*****.**', 'muxi304'))
        user_token = eval(user_token_json.data).get('token')
        u = User.query.filter_by(email='*****@*****.**').first()
        expect_token = u.generate_auth_token()
        self.assertTrue(str(user_token) == str(expect_token))

        res = self.client.get(url_for('api.get_comments'),
                              headers=self.get_token_headers(user_token))
        self.assertTrue(res.status_code == 200)
예제 #18
0
    def test_like_tip(self):
        """
        测试给相应tip点赞
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        admin_token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_tip'),
                               headers=self.get_token_headers(admin_token),
                               data=json.dumps({
                                   'title': 'test tips',
                                   'img_url':
                                   'http://substack.net/images/rov.png',
                                   'body': 'this is a test tip body, enen',
                                   'author': 'test app'
                               }))

        res = self.client.post(url_for('api.new_tips_id_like', id=1),
                               headers=self.get_token_headers(admin_token))
        likes = Tips.query.get_or_404(1).likes
        self.assertTrue(likes == 1)

        res = self.client.post(url_for('api.new_tips_id_like', id=1),
                               headers=self.get_token_headers(admin_token))
        self.assertTrue(res.status_code == 403)

        res = self.client.delete(url_for('api.new_tips_id_like', id=1),
                                 headers=self.get_token_headers(admin_token))
        likes = Tips.query.get_or_404(1).likes
        self.assertTrue(likes == 0)

        res = self.client.delete(url_for('api.new_tips_id_like', id=1),
                                 headers=self.get_token_headers(admin_token))
        self.assertTrue(res.status_code == 403)
예제 #19
0
파일: views.py 프로젝트: yuyilei/xueer_be
def privateship():
    """同步登录, token存入session"""
    email = request.args.get('email')
    user = User.query.filter_by(email=email).first()
    info = requests.get(current_app.config['MUXIAUTH'] + '/api/user/?email=%s' % email).json()
    username = info.get('username')
    password = '******'  # password placehold
    if user is None:
        u = User(
            username=username, email=email,
            password=base64.b64encode(password))
        db.session.add(u)
        db.session.commit()
    login_xueer_user(user)
    return redirect(url_for('hello.index'))
예제 #20
0
파일: manage.py 프로젝트: wanzifa/xueer_be
def adduser(username, email):
    """添加用户"""
    password = getpass('password ')
    confirm = getpass('confirm ')
    if password == confirm:
        u = User(
            email=email,
            username=username,
            password=base64.b64encode(password)
        )
        db.session.add(u)
        db.session.commit()
        print "user %s add in database! " % username
    else:
        print "password not confirmed!"
        exit(0)
예제 #21
0
    def test_statistics(self):
        # create an admin user
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()

        user_token_json = self.client.get(url_for('api.get_token'),
                                          headers=self.get_api_headers(
                                              '*****@*****.**', 'muxi304'))
        user_token = eval(user_token_json.data).get('token')

        res = self.client.get(url_for("api.get_statistics"),
                              headers=self.get_token_headers(user_token))
        self.assertTrue(res.status_code == 200)
예제 #22
0
    def test_about_user(self):
        """
        用户信息的增删改查
        """

        # 首先创建一个admin用户,并且获取该用户的token
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()

        user_token_json = self.client.get(url_for('api.get_token'),
                                          headers=self.get_api_headers(
                                              '*****@*****.**', 'muxi304'))
        user_token = eval(user_token_json.data).get('token')

        # 生成100条虚假用户信息
        User.generate_fake()

        # test admin get users
        res = self.client.get(url_for("api.get_users"),
                              headers=self.get_token_headers(user_token))
        self.assertTrue(res.status_code == 200)

        res = self.client.get(url_for("api.get_users", roleid=1),
                              headers=self.get_token_headers(user_token))
        self.assertTrue(res.status_code == 200)

        # test get user by id
        res = self.client.get(url_for("api.get_user_id", id=10))
        self.assertTrue(res.status_code == 200)

        res = self.client.get(url_for("api.get_user_id", id=1000))
        self.assertTrue(res.status_code == 404)

        # test admin create user
        res = self.client.post(url_for("api.new_user"),
                               headers=self.get_token_headers(user_token),
                               data=json.dumps({
                                   "username":
                                   "******",
                                   "password":
                                   b64encode("andrewpqc"),
                                   "email":
                                   "*****@*****.**",
                                   "roleid":
                                   3
                               }))
        self.assertTrue(res.status_code == 201)
        new_user_id = int(eval(res.data).get("id"))

        # update user info
        res = self.client.put(url_for("api.update_user", id=new_user_id),
                              headers=self.get_token_headers(user_token),
                              data=json.dumps({
                                  "email": "*****@*****.**",
                                  "roleid": 2
                              }))
        self.assertTrue(res.status_code == 200)
        u = User.query.get_or_404(new_user_id)
        # self.assertTrue(u.role_id == 2)
        self.assertTrue(u.email == "*****@*****.**")

        # test delete user
        res = self.client.delete(url_for("api.delete_user", id=new_user_id),
                                 headers=self.get_token_headers(user_token))
        self.assertTrue(res.status_code == 200)
예제 #23
0
    def test_category(self):
        """
        测试课程分类相关api
        由于在SetUp函数中已经生成了主分类,二级分类,学分分类相关数据,
        所以此处只测试获取,更新操作
        """
        # create an admin user
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()

        user_token_json = self.client.get(url_for('api.get_token'),
                                          headers=self.get_api_headers(
                                              '*****@*****.**', 'muxi304'))
        user_token = eval(user_token_json.data).get('token')

        # test get main category
        res1 = self.client.get(url_for("api.category"))
        self.assertTrue(res1.status_code == 200)

        # test admin create a main category
        res2 = self.client.post(url_for("api.new_category"),
                                headers=self.get_token_headers(user_token),
                                data=json.dumps({
                                    'category_name': 'test main category',
                                    'id': 7
                                }))
        self.assertTrue(res2.status_code == 201)
        self.assertTrue(eval(res2.data).get("test main category") == 7)

        # test admin to update a main category
        res3 = self.client.put(url_for("api.update_category", id=7),
                               headers=self.get_token_headers(user_token),
                               data=json.dumps(
                                   {'name': 'updated test main category'}))
        self.assertTrue(res3.status_code == 200)
        self.assertTrue(eval(res3.data).get("updated test main category") == 7)

        # test get sub category
        res4 = self.client.get(url_for('api.sub_category', main_category_id=1))
        self.assertTrue(res4.status_code == 200)

        # test admin create a sub category
        res5 = self.client.post(url_for("api.new_sub_category"),
                                headers=self.get_token_headers(user_token),
                                data=json.dumps({
                                    'name': 'test sub category',
                                    'id': 1
                                }))
        self.assertTrue(res5.status_code == 201)

        # test admin to update a sub category
        res6 = self.client.put(url_for("api.update_sub_category", id=1),
                               headers=self.get_token_headers(user_token),
                               data=json.dumps({
                                   'name': 'updated test main category',
                                   'main_category_id': 2
                               }))
        self.assertTrue(res6.status_code == 200)

        # test get credit category info
        res7 = self.client.get(url_for("api.credit_categories"))
        self.assertTrue(res7.status_code == 200)
예제 #24
0
    def test_write_course_comment(self):
        """
        测试编写课程评论,以及课程平均成绩的计算
        """
        u = User(email='*****@*****.**',
                 username='******',
                 password=b64encode('muxi304'),
                 role_id=2)
        db.session.add(u)
        db.session.commit()
        token = u.generate_auth_token()

        res = self.client.post(url_for('api.new_course'),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'name': 'test course',
                                   'teacher': 'test teacher',
                                   'category_id': 1,
                                   'type_id': 1,
                               }))

        res = self.client.get(url_for('api.get_course_id', id=1))
        average_final_score = json.loads(res.data).get("average_final_score")
        average_usual_score = json.loads(res.data).get("average_usual_score")
        self.assertTrue(average_usual_score == None)
        self.assertTrue(average_final_score == None)

        res = self.client.post(url_for('api.new_comment', id=1),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'body': 'this is a test comment',
                                   'tags': 'tag1 tag2',
                                   'final_score': 80,
                                   'usual_score': 70
                               }))
        print(res.data)
        self.assertTrue(res.status_code == 201)

        # test admin to get all comments
        res = self.client.get(url_for("api.get_comments"),
                              headers=self.get_token_headers(token))
        self.assertTrue(res.status_code == 200)

        # test login user to get comment by id
        res = self.client.get(url_for("api.get_id_comment", id=1),
                              headers=self.get_token_headers(token))
        self.assertTrue(res.status_code == 200)

        # test get all the comment of a course
        res = self.client.get(url_for("api.get_courses_id_comments", id=1),
                              headers=self.get_token_headers(token))
        self.assertTrue(res.status_code == 200)

        # test get hot comments
        res = self.client.get(url_for("api.get_hot_comments", id=1))
        self.assertTrue(res.status_code == 200)

        res = self.client.post(url_for('api.new_comment', id=1),
                               headers=self.get_token_headers(token),
                               data=json.dumps({
                                   'body': 'this is another test comment',
                                   'tags': 'tag1 tag2 tag3',
                                   'final_score': 90,
                                   'usual_score': 60
                               }))
        self.assertTrue(res.status_code == 201)

        res = self.client.get(url_for('api.get_course_id', id=1))
        average_final_score = json.loads(res.data).get("average_final_score")
        average_usual_score = json.loads(res.data).get("average_usual_score")
        self.assertTrue(average_usual_score == 65)
        self.assertTrue(average_final_score == 85)

        # test admin  to delete a comment
        res = self.client.delete(url_for("api.delete_comment", id=1),
                                 headers=self.get_token_headers(token))
        self.assertTrue(res.status_code == 200)