def test_comment_api(self): Auuid = uuid.uuid4() post_uuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user, uuid=Auuid) author.save() post = Post(uuid=post_uuid, author=author, title="test post", content="this is a test") post.save() comment = Comment(author=author, post=post, content="Test Comment") comment.save() self.client.login(username=user.username, password="******") ApiUrl = reverse("api:post_comments", args=[post_uuid]) #Test GET method. The GET method returns a list of comments on a specific post GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("count"), 1) self.assertEqual( GetResponse.data.get("comments")[0].get("comment"), "Test Comment")
def main(request): if request.method == 'POST': if request.POST.get('image') == '': image_id = '' else: ret = cloudinary.uploader.upload(request.FILES['image'], sign_url=True) image_id = ret['public_id'] if request.POST['content'] != '': post = Post(author=Author.objects.get(user=request.user), title=request.POST['post-title'], content=request.POST['content'], markdown=request.POST.get('markdown', False), image=image_id, visibility=request.POST['visibility']) if request.POST['visibility'] == 'ONL': share_to_author = Author.objects.get(uuid=request.POST['share_to']) post.user_can_view = share_to_author post.save() return HttpResponseRedirect(reverse('socialp2p:main')) else: return HttpResponse("Can't post an empty post") else: data = [] nodes = Node.objects.all() endpoint = '/posts/' for node in nodes: url = node.host + endpoint r = requests.get(url, auth=(node.access_username, node.access_password)) if r.status_code == 200: p = r.json().get('posts') data += p author = Author.objects.get(user=request.user) private_posts = Post.objects.filter(author=author, visibility='PRIVATE') my_friend_posts = Post.objects.filter(author=author, visibility='FRIENDS') my_other_posts = Post.objects.filter(author=author, visibility='ONL') local_public_posts = Post.objects.filter(author=author, visibility='SERVERONLY') posts = Post.objects.filter(visibility='PUBLIC') posts = posts | private_posts | my_friend_posts | my_other_posts | local_public_posts for i in author.friends.all(): friends_posts = Post.objects.filter(author=i, visibility='FRIENDS') friends_fof_posts = Post.objects.filter(author=i, visibility='FOAF') posts = posts | friends_posts | friends_fof_posts for fof in i.friends.all(): fof_posts = Post.objects.filter(author=fof, visibility='FOAF') posts = posts | fof_posts ONL_posts = Post.objects.filter(user_can_view=author, visibility='ONL') posts = posts | ONL_posts posts = posts.order_by('-datetime') authors = Author.objects.order_by('user__username') current_author = Author.objects.get(user=request.user) comments = Comment.objects.order_by('-datetime') return render(request, 'socialp2p/main.html', {'Post': Post, 'posts': posts, 'authors': authors, 'current_author': current_author, 'comments': comments, 'remote_posts':data})
def test_post(self): Postuuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user) author.save() posts = Post(author=author, title="test post", content="this is a test", visibility="PUBLIC", uuid=Postuuid) posts.save() self.assertEqual(Post.objects.count(), 1) self.assertEqual(posts.visibility, "PUBLIC") self.assertEqual(posts.uuid, Postuuid)
def test_comment(self): Commentuuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user) author.save() posts = Post(author=author, title="test post", content="this is a test", visibility="PUBLIC") posts.save() comment = Comment(author=author, post=posts, uuid=Commentuuid) comment.save() self.assertEqual(Comment.objects.count(), 1) self.assertEqual(comment.uuid, Commentuuid)
def test_comment_api(self): Auuid = uuid.uuid4() post_uuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user, uuid=Auuid) author.save() post = Post(uuid=post_uuid, author=author, title="test post", content="this is a test") post.save() comment = Comment(author=author, post=post, content="Test Comment") comment.save() self.client.login(username=user.username, password="******") ApiUrl = reverse("api:post_comments", args=[post_uuid]) #Test GET method. The GET method returns a list of comments on a specific post GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("count"), 1) self.assertEqual(GetResponse.data.get("comments")[0].get("comment"), "Test Comment")
def test_post_api(self): Auuid = uuid.uuid4() anouther_uuid = uuid.uuid4() post_uuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") user.save() author = Author(user=user, uuid=Auuid) author.save() user2 = User.objects.create_user("test2", "*****@*****.**", "testpassword") user2.save() author2 = Author(user=user2, uuid=anouther_uuid) author2.save() posts = Post(uuid=post_uuid, author=author, title="test post", content="this is a test", visibility="PUBLIC") posts.save() #create public post to test if the currently authenticated can see it or not posts2 = Post(author=author2, title="test public post", content="this is a test", visibility="PUBLIC") posts2.save() #create private post to test if the currently authenticated can see it or not posts2 = Post(author=author2, title="test private post", content="this is a test", visibility="PRIVATE") posts2.save() self.client.login(username=user.username, password="******") ApiUrl = reverse("api:posts") #Test GET method. The GET method returns a list of posts that currently authenticated user can see. #Should only see the two public posts GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("count"), 2) #Test GET method of author_posts. The GET method returns a list of posts made by a specific author that the currently authenticated #can see. Should see only one post. ApiUrl = reverse("api:author_posts", args=[anouther_uuid]) GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("count"), 1) self.assertEqual(GetResponse.data.get("posts")[0].get("title"), "test public post") #Test GET method of post_detail. The GET method return a specific post ApiUrl = reverse("api:post_detail", args=[post_uuid]) GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("posts")[0].get("id"), str(post_uuid))
def main(request): if request.method == 'POST': if request.POST.get('image') == '': image_id = '' else: ret = cloudinary.uploader.upload(request.FILES['image'], sign_url=True) image_id = ret['public_id'] if request.POST['content'] != '': post = Post(author=Author.objects.get(user=request.user), title=request.POST['post-title'], content=request.POST['content'], markdown=request.POST.get('markdown', False), image=image_id, visibility=request.POST['visibility']) if request.POST['visibility'] == 'ONL': share_to_author = Author.objects.get( uuid=request.POST['share_to']) post.user_can_view = share_to_author post.save() return HttpResponseRedirect(reverse('socialp2p:main')) else: return HttpResponse("Can't post an empty post") else: data = [] nodes = Node.objects.all() endpoint = '/posts/' for node in nodes: url = node.host + endpoint r = requests.get(url, auth=(node.access_username, node.access_password)) if r.status_code == 200: p = r.json().get('posts') data += p author = Author.objects.get(user=request.user) private_posts = Post.objects.filter(author=author, visibility='PRIVATE') my_friend_posts = Post.objects.filter(author=author, visibility='FRIENDS') my_other_posts = Post.objects.filter(author=author, visibility='ONL') local_public_posts = Post.objects.filter(author=author, visibility='SERVERONLY') posts = Post.objects.filter(visibility='PUBLIC') posts = posts | private_posts | my_friend_posts | my_other_posts | local_public_posts for i in author.friends.all(): friends_posts = Post.objects.filter(author=i, visibility='FRIENDS') friends_fof_posts = Post.objects.filter(author=i, visibility='FOAF') posts = posts | friends_posts | friends_fof_posts for fof in i.friends.all(): fof_posts = Post.objects.filter(author=fof, visibility='FOAF') posts = posts | fof_posts ONL_posts = Post.objects.filter(user_can_view=author, visibility='ONL') posts = posts | ONL_posts posts = posts.order_by('-datetime') authors = Author.objects.order_by('user__username') current_author = Author.objects.get(user=request.user) comments = Comment.objects.order_by('-datetime') return render( request, 'socialp2p/main.html', { 'Post': Post, 'posts': posts, 'authors': authors, 'current_author': current_author, 'comments': comments, 'remote_posts': data })
def test_post_api(self): Auuid = uuid.uuid4() anouther_uuid = uuid.uuid4() post_uuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") user.save() author = Author(user=user, uuid=Auuid) author.save() user2 = User.objects.create_user("test2", "*****@*****.**", "testpassword") user2.save() author2 = Author(user=user2, uuid=anouther_uuid) author2.save() posts = Post(uuid=post_uuid, author=author, title="test post", content="this is a test", visibility="PUBLIC") posts.save() #create public post to test if the currently authenticated can see it or not posts2 = Post(author=author2, title="test public post", content="this is a test", visibility="PUBLIC") posts2.save() #create private post to test if the currently authenticated can see it or not posts2 = Post(author=author2, title="test private post", content="this is a test", visibility="PRIVATE") posts2.save() self.client.login(username=user.username, password="******") ApiUrl = reverse("api:posts") #Test GET method. The GET method returns a list of posts that currently authenticated user can see. #Should only see the two public posts GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("count"), 2) #Test GET method of author_posts. The GET method returns a list of posts made by a specific author that the currently authenticated #can see. Should see only one post. ApiUrl = reverse("api:author_posts", args=[anouther_uuid]) GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("count"), 1) self.assertEqual( GetResponse.data.get("posts")[0].get("title"), "test public post") #Test GET method of post_detail. The GET method return a specific post ApiUrl = reverse("api:post_detail", args=[post_uuid]) GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual( GetResponse.data.get("posts")[0].get("id"), str(post_uuid))