예제 #1
0
def login(request):
    #raw: [{"username": "******", "password" : "12345"}]
    #print "----login-----"
    data = json.loads(request.body)
    #print data
    username = data[0]['username']
    password = data[0]['password']
    #print username
    #print password

    querySet = User.objects.filter(username__exact = username, password__exact = password)

    if any(querySet):
        for e in querySet:
            user = User(id=e.id, username=e.username, password=e.password, age=e.age, sex=e.sex, email=e.email)
            user.save()
            # print user
            # print UserEncoder().encode(user)
            userStr = json.dumps(user, cls= UserEncoder)
            # print userStr

        return Response({
            "data": userStr
        },status=status.HTTP_202_ACCEPTED)
    else:
        return Response({
            "data": ""
        },status=status.HTTP_404_NOT_FOUND)
예제 #2
0
파일: tests.py 프로젝트: laurentpd/Backend
    def setUp(self):
        # Set up a test user
        self.user = User(email="*****@*****.**", username="******")
        self.user.set_password("TEST")
        self.user.save()

        # Set up an author
        self.author = User(email="*****@*****.**", username="******")
        self.author.set_password("TEST")
        self.author.save()

        # Test problem
        self.problem = Problem(
            title="Factorial",
            author=self.author,
            description="Write the factorial function fact(n)",
            difficulty=1)
        self.problem.rating = 5  # Manually set this
        self.problem.save()

        self.rating = Rating(message="Hello",
                             rating=5,
                             content="Awesome Sauce",
                             rating_of=self.problem,
                             reviewer=self.user)
        self.rating.save()
예제 #3
0
def initdb():
    dba.create_all()
    dba.session.add(
        User(public_id=str(uuid.uuid4()), name='Eric', email='*****@*****.**'))
    dba.session.add(
        User(public_id=str(uuid.uuid4()), name='Chen', email='*****@*****.**'))
    dba.session.commit()
    print 'Initialized the database'
예제 #4
0
def admin_user(db):
    user = User(username='******', email='*****@*****.**', password='******')

    db.session.add(user)
    db.session.commit()

    return user
예제 #5
0
파일: tests.py 프로젝트: laurentpd/Backend
    def setUp(self):
        # Set up a test user
        self.user = User(email="*****@*****.**", username="******")
        self.user.set_password("TEST")
        self.user.save()

        # Set up an author
        self.author = User(email="*****@*****.**", username="******")
        self.author.set_password("TEST")
        self.author.save()

        # Test problem
        self.problem = Problem(
            title="Factorial",
            author=self.author,
            description="Write the factorial function fact(n)",
            difficulty=1)
        self.problem.save()

        test_case = TestCase(method="fact",
                             inputs="[5]",
                             outputs="[120]",
                             problem=self.problem)
        test_case.save()

        test_case2 = TestCase(method="fact",
                              inputs="[0]",
                              outputs="[1]",
                              problem=self.problem)
        test_case2.save()

        test_case3 = TestCase(method="fact",
                              inputs="[4]",
                              outputs="[24]",
                              problem=self.problem)
        test_case3.save()
예제 #6
0
def create_user(current_user):

    if not current_user.admin:
        return jsonify({'message': 'Cannot perform that function'})

    data = request.get_json()
    hashed_password = bcrypt.generate_password_hash(
        data['password']).decode('utf-8')

    user = User(public_id=str(uuid.uuid4()),
                name=data['name'],
                password=hashed_password,
                admin=False)
    db.session.add(user)
    db.session.commit()
    return jsonify({'message': 'User Added Successfully!'})
예제 #7
0
def create():
    #verify JWT
    if not all(arg in list(request.args.keys()) for arg in ["username","email","password"]):
        response = make_response(jsonify({"status":"Error"}), 400)
    else:
        #Verify the JWT/session sent in the headers, if verified proceed
        password_encode = b64encode(bytes(request.args['password'],'utf-8'))
        user = User(username=request.args['username'], email=request.args['email'], password=password_encode)
        if UserUtils.check_user(user):
            result = {"status":"user already exists"}
            code = 404
        else:
            UserUtils.insert_user(user)
            result = {"status":"user created"}
            code=200
        response = make_response(jsonify(result), code)
    return response
예제 #8
0
def init():
    """Init application, create database tables
    and create a new user named admin with password admin
    """
    from restapi.extensions import db
    from restapi.models import User
    click.echo("create database")
    db.create_all()
    click.echo("done")

    click.echo("create user")
    user = User(username='******',
                email='*****@*****.**',
                password='******',
                active=True)
    db.session.add(user)
    db.session.commit()
    click.echo("created user admin")
예제 #9
0
def user_factory(i):
    return User(
        username="******".format(i),
        email="user{}@mail.com".format(i)
    )
예제 #10
0
파일: tests.py 프로젝트: laurentpd/Backend
class SolutionsAPITestCase(JWTAPITestCase):
    def setUp(self):
        # Set up a test user
        self.user = User(email="*****@*****.**", username="******")
        self.user.set_password("TEST")
        self.user.save()

        # Set up an author
        self.author = User(email="*****@*****.**", username="******")
        self.author.set_password("TEST")
        self.author.save()

        # Test problem
        self.problem = Problem(
            title="Factorial",
            author=self.author,
            description="Write the factorial function fact(n)",
            difficulty=1)
        self.problem.save()

        test_case = TestCase(method="fact",
                             inputs="[5]",
                             outputs="[120]",
                             problem=self.problem)
        test_case.save()

        test_case2 = TestCase(method="fact",
                              inputs="[0]",
                              outputs="[1]",
                              problem=self.problem)
        test_case2.save()

        test_case3 = TestCase(method="fact",
                              inputs="[4]",
                              outputs="[24]",
                              problem=self.problem)
        test_case3.save()

    def test_create(self):
        url = reverse("restapi:solutions-listcreate",
                      kwargs={"problem_id": self.problem.id})

        # Authenticate as a user
        self.authenticate(self.user)

        # Submit the following solution
        data = {
            "code":
            """
def fact(n):
    if n <= 1:
        return 1
    else:
        return n * fact(n-1)

            """
        }
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        solution = Solution.objects.first()
        self.assertIsNotNone(solution,
                             msg="A solution object should've been created")

        # There should be one job for each test case return in response.data
        self.assertEqual(len(response.data["jobs"]),
                         3,
                         msg="There should be one job planned")

        jobs = TestCaseJob.objects.filter(job_ptr__solution=solution)

        # Manually run all jobs (without RQ)
        for job in jobs:
            self.assertIsInstance(job, TestCaseJob)
            job.run()
            self.assertTrue(job.success)

    def test_retrieve(self):
        solution = Solution(code="Blah",
                            language="python",
                            problem=self.problem)
        solution.save()

        url = solution.get_absolute_url()
        response = self.client.get(url, data={}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
예제 #11
0
파일: tests.py 프로젝트: laurentpd/Backend
class RatingAPITestCase(JWTAPITestCase):
    def setUp(self):
        # Set up a test user
        self.user = User(email="*****@*****.**", username="******")
        self.user.set_password("TEST")
        self.user.save()

        # Set up an author
        self.author = User(email="*****@*****.**", username="******")
        self.author.set_password("TEST")
        self.author.save()

        # Test problem
        self.problem = Problem(
            title="Factorial",
            author=self.author,
            description="Write the factorial function fact(n)",
            difficulty=1)
        self.problem.rating = 5  # Manually set this
        self.problem.save()

        self.rating = Rating(message="Hello",
                             rating=5,
                             content="Awesome Sauce",
                             rating_of=self.problem,
                             reviewer=self.user)
        self.rating.save()

    def test_listcreate(self):
        uri = reverse('restapi:ratings-listcreate',
                      kwargs={'problem_id': self.problem.pk})

        # Test creation
        data = {
            "message": "Hi",
            "rating": 3,
            "content": "This is pretty cool dude",
        }
        response = self.client.post(uri, data, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_401_UNAUTHORIZED,
            msg=
            "User should be authenticated before being able to rate problems")

        self.authenticate(self.user)
        response = self.client.post(uri, data, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
            msg="An authenticated user should be able to rate problems")

        response = self.client.get(uri, data={}, format='json')
        self.assertEqual(len(response.data),
                         2,
                         msg="There should now be two list entries")

        # Test that the ratings have averaged out
        self.assertAlmostEqual(Problem.objects.first().rating, 4)

    def test_retrieve(self):
        rating = Rating.objects.first()
        uri = rating.get_absolute_url()

        response = self.client.get(uri, data={}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["id"], rating.id)

    def test_update(self):
        rating = Rating.objects.first()
        uri = rating.get_absolute_url()

        self.authenticate(self.author)

        data = {
            "message": "Hello",
            "rating": 2,
            "content": "Not so awesome sauce",
        }
        response = self.client.put(uri, data, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_403_FORBIDDEN,
            msg="Only the reviewer should be able to update a rating")

        self.authenticate(self.user)
        response = self.client.put(uri, data, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_200_OK,
            msg="The reviewer should be able to update his rating")

        self.assertAlmostEqual(
            Problem.objects.first().rating,
            2,
            msg="The rating of the problem should've been updated to 2")

    def test_delete(self):
        rating = Rating.objects.first()
        uri = rating.get_absolute_url()

        self.authenticate(self.author)
        response = self.client.delete(uri, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_403_FORBIDDEN,
            msg="Only the reviewer should be able to delete a rating")

        self.authenticate(self.user)
        response = self.client.delete(uri, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_204_NO_CONTENT,
            msg="The review should be able to delete their rating")

        self.assertIsNone(
            Problem.objects.first().rating,
            msg=
            "The problem rating should be made Null since there are no more ratings"
        )

    def test_running_average(self):
        # Reset everything
        Rating.objects.first().delete()
        Problem.objects.first().rating = None

        url = reverse("restapi:ratings-listcreate",
                      kwargs={'problem_id': Problem.objects.first().id})

        self.authenticate(self.user)

        template = {
            "message": "Hi",
            "rating": 5,
            "content": "Yo nice job on this problem it's awesome",
        }
        self.client.post(url, data=template, format='json')
        self.assertAlmostEqual(Problem.objects.first().rating, 5)

        template["rating"] = 1
        self.client.post(url, data=template, format='json')
        self.assertAlmostEqual(Problem.objects.first().rating, 3)

        template["rating"] = 2
        self.client.post(url, data=template, format='json')
        self.assertAlmostEqual(Problem.objects.first().rating, 8 / 3)

        # Change the 2nd rating to 2, average should go to 9 / 3 = 3
        put_url = Rating.objects.all()[1].get_absolute_url()
        self.client.put(put_url, data=template, format='json')
        self.assertAlmostEqual(Problem.objects.first().rating, 3)

        # Delete the 1st rating, average should go to 4 / 2 = 2
        delete_url = Rating.objects.first().get_absolute_url()
        self.client.delete(delete_url, format='json')
        self.assertAlmostEqual(Problem.objects.first().rating, 2)
예제 #12
0
파일: tests.py 프로젝트: laurentpd/Backend
    def setUp(self):
        # Set up a test user
        user = User(email="*****@*****.**", username="******")
        user.set_password("TEST")
        user.save()

        # Set up an author
        author = User(email="*****@*****.**", username="******")
        author.set_password("TEST")
        author.save()

        # Test problem
        self.problem = Problem(
            title="Factorial",
            author=author,
            description="Write the factorial function fact(n)",
            difficulty=1)
        self.problem.save()
예제 #13
0
파일: tests.py 프로젝트: laurentpd/Backend
class TestCaseAPITestCase(JWTAPITestCase):
    def setUp(self):
        # Set up a test user
        self.user = User(email="*****@*****.**", username="******")
        self.user.set_password("TEST")
        self.user.save()

        # Set up an author
        self.author = User(email="*****@*****.**", username="******")
        self.author.set_password("TEST")
        self.author.save()

        # Test problem
        self.problem = Problem(
            title="Factorial",
            author=self.author,
            description="Write the factorial function fact(n)",
            difficulty=1)
        self.problem.save()

        test_case = TestCase(method="fact",
                             inputs="[5]",
                             outputs="[120]",
                             problem=self.problem)
        test_case.save()

    def test_list_create(self):
        uri = reverse('restapi:testcases-listcreate',
                      kwargs={'problem_id': self.problem.pk})

        # Test creation
        data = {
            "method": "fact",
            "inputs": "[4]",
            "outputs": "[24]",
        }
        self.authenticate(self.user)
        response = self.client.post(uri, data, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_403_FORBIDDEN,
            msg=
            "Only the author of the problem should be able to add test cases")

        self.authenticate(self.author)
        response = self.client.post(uri, data, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
            msg="The author should be able to successfully create the test case"
        )

        response = self.client.get(uri, data={}, format='json')
        self.assertEqual(len(response.data),
                         2,
                         msg="There should now be two list entries")

    def test_retrieve(self):
        test_case = TestCase.objects.first()
        uri = test_case.get_absolute_url()

        response = self.client.get(uri, data={}, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEquals(test_case.id, response.data["id"])

    def test_update(self):
        test_case = TestCase.objects.first()
        url = test_case.get_absolute_url()

        data = {
            "method": "fact",
            "input": "[0]",
            "output": "[1]",
        }
        self.authenticate(self.user)
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

        self.authenticate(self.author)
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_delete(self):
        test_case = TestCase.objects.first()
        url = test_case.get_absolute_url()

        self.authenticate(self.user)
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

        self.authenticate(self.author)
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)