<commit_before>from rest_framework.test import APITestCase, APIClient, force_authenticate
from models.Post import Post
from models.Author import Author
from django.contrib.auth.models import User
import uuid
from django.core import serializers
import json
from unittest import skip

class PostAPITests(APITestCase):
    def setUp(self):
        superuser = User.objects.create_superuser('superuser', '<EMAIL>', 'test1234')
        self.client = APIClient()
        #Authenticate as a super user so we can test everything
        self.client.force_authenticate(user=superuser)
        self.author = Author.create(host='local', displayName='testMonkey', user=superuser)
        self.author.save()
        self.post = Post.create(self.author,
            title="A post title about a post about web dev",
            origin="http://whereitcamefrom.com/post/zzzzz",
            description="This post discusses stuff -- brief",
            categories = ["web","tutorial"],
            visibility = "PUBLIC")
        self.post.save()

    def get_posts_by_current_user(self):
        return self.client.get('/author/posts/')
        #http://service/author/posts (posts that are visible to the currently authenticated user)

    def get_public_posts(self):
        # http://service/posts (all posts marked as public on the server)
        return self.client.get('/posts/')

    def get_posts_by_author_id(self, author_id):
        # http://service/author/{AUTHOR_ID}/posts (all posts made by {AUTHOR_ID} visible to the currently authenticated user)
        return self.client.get('/author/'+str(author_id)+'/posts/')

    def get_posts_by_page(self, page_number):
        # GET http://service/author/posts?page=4
        return self.client.get('/author/posts?page='+str(page_number)+'/')

    def get_posts_by_page_and_size(self, page_number, size):
        # GET http://service/author/posts?page=4&size=50
        return self.client.get('/author/posts?page='+str(page_number)+'&size='+str(size)+'/')

    def get_single_post_by_id(self, post_id):
        # http://service/posts/{POST_ID} access to a single post with id = {POST_ID}
        return self.client.get('/posts/'+str(post_id)+'/')

    def create_post(self, post_body):
        #a POST should insert the post http://service/posts/postid
        return self.client.post('/posts/', data=post_body, format='json')

    def delete_post(self, post_id):
        #A delete should delete posts with a specific ID
        return self.client.delete('/posts/'+str(post_id)+'/')

    def create_update_post_with_put(self, post_id, put_body):
        #PUT http://service/posts/postid to update/create post
        return self.client.put('/posts/'+str(post_id)+'/', put_body, format='json')

    def create_update_post_with_post(self, post_id, post_body):
        return self.client.post('/posts/'+str(post_id)+'/', post_body, format='json')


    def test_get_posts_by_current_author(self):
        response = self.get_posts_by_current_user()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(str(self.post.id), response.data[0]['id'])
        self.assertEqual(str(self.post.author.id), response.data[0]['author']['id'])

    def test_no_posts_by_current_author(self):
        post_id = self.post.id
        # create posts when needed instead
        #self.delete_post(post_id)
        response = self.get_posts_by_current_user()
        self.assertEqual(response.status_code, 200)
        #self.assertEqual(len(response.data), 0)

    def test_get_posts_by_current_admin(self):
        #TODO: Retrieves all the posts made by the currently logged in admin
        #how is it different from test_get_posts_by_current_author?
        pass

    def test_get_public_posts(self):
        # Retrieve all public posts on the server
        response = self.get_public_posts()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data[0]['visibility'], "PUBLIC")

    def test_get_posts_by_author_id(self):
        # Return all the posts made by an author with a specific ID
        response = self.get_posts_by_author_id(self.author.id)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(str(self.post.id), response.data[0]['id'])
        self.assertEqual(str(self.post.author.id), response.data[0]['author']['id'])

    def test_get_posts_with_invalid_author_id(self):
        # Get posts with an invalid author id, or author ID that doesn't exist
        not_author = uuid.uuid4()
        response = self.get_posts_by_author_id(not_author)
        self.assertEqual(response.status_code, 404)

    def test_get_post_by_id(self):
        response = self.get_single_post_by_id(self.post.id)
        self.assertEqual(200, response.status_code)
        self.assertEqual(str(self.post.id), response.data['id'])
        self.assertEqual(str(self.post.author.id), response.data['author']['id'])

    def test_get_posts_with_invalid_post_id(self):
        #TODO: tests behaviour for when you get posts with incorrectly
        #formatted ID, or ID that doesn't exist
        pass<commit_msg>Added a test for getting a nonexistent post.<commit_after>from rest_framework.test import APITestCase, APIClient, force_authenticate
from models.Post import Post
from models.Author import Author
from django.contrib.auth.models import User
import uuid
from django.core import serializers
import json
from unittest import skip

class PostAPITests(APITestCase):
    def setUp(self):
        superuser = User.objects.create_superuser('superuser', '<EMAIL>', 'test1234')
        self.client = APIClient()
        #Authenticate as a super user so we can test everything
        self.client.force_authenticate(user=superuser)
        self.author = Author.create(host='local', displayName='testMonkey', user=superuser)
        self.author.save()
        self.post = Post.create(self.author,
            title="A post title about a post about web dev",
            origin="http://whereitcamefrom.com/post/zzzzz",
            description="This post discusses stuff -- brief",
            categories = ["web","tutorial"],
            visibility = "PUBLIC")
        self.post.save()

    def get_posts_by_current_user(self):
        return self.client.get('/author/posts/')
        #http://service/author/posts (posts that are visible to the currently authenticated user)

    def get_public_posts(self):
        # http://service/posts (all posts marked as public on the server)
        return self.client.get('/posts/')

    def get_posts_by_author_id(self, author_id):
        # http://service/author/{AUTHOR_ID}/posts (all posts made by {AUTHOR_ID} visible to the currently authenticated user)
        return self.client.get('/author/'+str(author_id)+'/posts/')

    def get_posts_by_page(self, page_number):
        # GET http://service/author/posts?page=4
        return self.client.get('/author/posts?page='+str(page_number)+'/')

    def get_posts_by_page_and_size(self, page_number, size):
        # GET http://service/author/posts?page=4&size=50
        return self.client.get('/author/posts?page='+str(page_number)+'&size='+str(size)+'/')

    def get_single_post_by_id(self, post_id):
        # http://service/posts/{POST_ID} access to a single post with id = {POST_ID}
        return self.client.get('/posts/'+str(post_id)+'/')

    def create_post(self, post_body):
        #a POST should insert the post http://service/posts/postid
        return self.client.post('/posts/', data=post_body, format='json')

    def delete_post(self, post_id):
        #A delete should delete posts with a specific ID
        return self.client.delete('/posts/'+str(post_id)+'/')

    def create_update_post_with_put(self, post_id, put_body):
        #PUT http://service/posts/postid to update/create post
        return self.client.put('/posts/'+str(post_id)+'/', put_body, format='json')

    def create_update_post_with_post(self, post_id, post_body):
        return self.client.post('/posts/'+str(post_id)+'/', post_body, format='json')


    def test_get_posts_by_current_author(self):
        response = self.get_posts_by_current_user()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(str(self.post.id), response.data[0]['id'])
        self.assertEqual(str(self.post.author.id), response.data[0]['author']['id'])

    def test_no_posts_by_current_author(self):
        post_id = self.post.id
        # create posts when needed instead
        #self.delete_post(post_id)
        response = self.get_posts_by_current_user()
        self.assertEqual(response.status_code, 200)
        #self.assertEqual(len(response.data), 0)

    def test_get_posts_by_current_admin(self):
        #TODO: Retrieves all the posts made by the currently logged in admin
        #how is it different from test_get_posts_by_current_author?
        pass

    def test_get_public_posts(self):
        # Retrieve all public posts on the server
        response = self.get_public_posts()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data[0]['visibility'], "PUBLIC")

    def test_get_posts_by_author_id(self):
        # Return all the posts made by an author with a specific ID
        response = self.get_posts_by_author_id(self.author.id)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(str(self.post.id), response.data[0]['id'])
        self.assertEqual(str(self.post.author.id), response.data[0]['author']['id'])

    def test_get_posts_with_invalid_author_id(self):
        # Get posts with an invalid author id, or author ID that doesn't exist
        not_author = uuid.uuid4()
        response = self.get_posts_by_author_id(not_author)
        self.assertEqual(response.status_code, 404)

    def test_get_post_by_id(self):
        response = self.get_single_post_by_id(self.post.id)
        self.assertEqual(200, response.status_code)
        self.assertEqual(str(self.post.id), response.data['id'])
        self.assertEqual(str(self.post.author.id), response.data['author']['id'])

    def test_get_nonexistent_post_by_id(self):
        self.post.id = uuid.uuid4()
        response = self.get_single_post_by_id(self.post.id)
        self.assertEqual(404, response.status_code)

    def test_get_posts_with_invalid_post_id(self):
        #TODO: tests behaviour for when you get posts with incorrectly
        #formatted ID, or ID that doesn't exist
        pass
{'fraction_non_alphanumeric': 0.11799099012748011, 'fraction_numerical': 0.006709479536087415, 'mean_word_length': 3.384033613445378, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2350260', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3119, 'n_words': 684}