def signup_view(request): if request.user.is_authenticated(): return HttpResponseRedirect(reverse('socialp2p:main')) if request.method == 'POST': if User.objects.filter(username=request.POST['username']).exists(): return HttpResponse("Username already taken") user = User.objects.create_user(request.POST['username'], None, request.POST['password']) user.is_active = False user.save() author = Author(user=user) author.save() a_user = authenticate(username=request.POST['username'], password=request.POST['password']) if a_user is not None: if user.is_active: login(request, a_user) return HttpResponseRedirect(reverse('socialp2p:main')) else: return HttpResponseRedirect(reverse('socialp2p:signup')) return HttpResponseRedirect(reverse('socialp2p:signup')) else: return render(request, 'socialp2p/signup.html')
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_friends_api(self): Auuid = uuid.uuid4() Frienduuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user, uuid=Auuid) author.save() friend = User.objects.create_user("testFriend", "*****@*****.**", "FriendPassword") Friendauthor = Author(user=friend, uuid=Frienduuid) Friendauthor.save() author.friends.add(Friendauthor) author.save() self.client.login(username=user.username, password="******") ApiUrl = reverse("api:friends", args=[Auuid]) #Test GET method. The GET method returns a list of friends GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get("authors")[0], Frienduuid)
def test_author_list_api(self): Auuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user, uuid=Auuid) author.save() self.client = APIClient() self.client.login(username="******", password="******") ApiUrl = reverse("api:author_list") response = self.client.get(ApiUrl) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[0].get('id'), str(author.uuid))
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_author_api(self): Auuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user, uuid=Auuid) author.save() ApiUrl = reverse("api:author_detail", args=[Auuid]) self.client.login(username="******", password="******") #Test GET method. The GET method returns a specific author GetResponse = self.client.get(ApiUrl) self.assertEqual(GetResponse.status_code, status.HTTP_200_OK) self.assertEqual(GetResponse.data.get('id'), str(author.uuid)) #Test DELETE method. The DELETE method delete the author DeleteResponse = self.client.delete(ApiUrl) self.assertEqual(DeleteResponse.status_code, status.HTTP_204_NO_CONTENT)
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_friend_request(self): ReqUuid = uuid.uuid4() RecvUuid = uuid.uuid4() ReqUser = User.objects.create_user("Requester", "*****@*****.**", "testpassword") ReqAuthor = Author(user=ReqUser, uuid=ReqUuid) ReqAuthor.save() RecvUser = User.objects.create_user("Receiver", "*****@*****.**", "testpassword") RecvAuthor = Author(user=RecvUser, uuid=RecvUuid) RecvAuthor.save() friendrequest = FriendRequest(requester=ReqAuthor, receiver=RecvAuthor) friendrequest.save() self.assertEqual(FriendRequest.objects.count(), 1) self.assertEqual(friendrequest.requester.uuid, ReqUuid) self.assertEqual(friendrequest.receiver.uuid, RecvUuid)
def test_create_author(self): Auuid = uuid.uuid4() Frienduuid = uuid.uuid4() user = User.objects.create_user("test", "*****@*****.**", "testpassword") author = Author(user=user, uuid=Auuid) author.save() friend = User.objects.create_user("testFriend", "*****@*****.**", "FriendPassword") Friendauthor = Author(user=friend, uuid=Frienduuid) Friendauthor.save() author.friends.add(Friendauthor) self.assertEqual(User.objects.count(), 2) self.assertEqual(Author.objects.count(), 2) self.assertEqual(author.uuid, Auuid) self.assertEqual(author.friends.count(), 1)
def test_friend_request_api(self): ReqUuid = uuid.uuid4() RecvUuid = uuid.uuid4() ReqUser = User.objects.create_user("Requester", "*****@*****.**", "testpassword") ReqAuthor = Author(user=ReqUser, uuid=ReqUuid) ReqAuthor.save() self.client.login(username=ReqUser.username, password="******") RecvUser = User.objects.create_user("Receiver", "*****@*****.**", "testpassword") RecvAuthor = Author(user=RecvUser, uuid=RecvUuid) RecvAuthor.save() friendrequest = FriendRequest(requester=ReqAuthor, receiver=RecvAuthor) friendrequest.save() serializer = FriendRequestSerializer(friendrequest) ApiUrl = reverse("api:friend_request")
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 friend_request(request): #current_user = User.objects.get(id=request.user.id) #current_author = Author.objects.get(user=current_user) #try: #author = Author.objects.get(uuid=author_uuid) #except Author.DoesNotExist: #return Response(status=status.HTTP_404_NOT_FOUND) #user = User.objects.get(author=author) #if request.method == 'GET': #requests = FriendRequest.objects.filter(requester=current_author, accepted=False) #serializer = FriendRequestSerializer(requests, many=True) #return Response(serializer.data) if request.method == 'POST': if request.is_ajax(): local = False user_uuid = json.loads(request.POST.get('author')) author_uuid = json.loads(request.POST.get('friend')) author_host = json.loads(request.POST.get('host')) if request.POST.get('displayname'): author_name = request.POST.get('displayname') if request.POST.get('displayName'): author_name = request.POST.get('displayName') author_url = request.POST.get('author_url') current_author = Author.objects.get(uuid=user_uuid) if Author.objects.filter(uuid=author_uuid).exists(): friend = Author.objects.get(uuid=author_uuid) local = True if local: if FriendRequest.objects.filter(requester=current_author, receiver=friend).exists(): return HttpResponse("Already added Friend") else: friendRequest = FriendRequest(requester=current_author, receiver=friend) friendRequest.save() serializer = FriendRequestSerializer(friendRequest) return Response(serializer.data, status=status.HTTP_200_OK) else: nodes = Node.objects.all() tempuser = User(username=author_name, password='******') tempauthor = Author(user=tempuser, uuid=author_uuid, host=author_host) friendRequest = FriendRequest(requester=current_author, receiver=tempauthor) serializer = FriendRequestSerializer(friendRequest) if author_host == 'project-c404.rhcloud.com/api': endpoint = '/friendrequest' auth_host_url = 'http://' + author_host else: endpoint = '/api/friendrequest' auth_host_url = 'http://' + author_host + '/api' url = 'http://' + author_host + endpoint for node in nodes: if node.host == auth_host_url: print "URL: ", url headers = {'Content-type': 'application/json'} r = requests.post(url, auth=(node.access_username, node.access_password), json=serializer.data, headers=headers) print "RESPONSE TEXT: ", r.text print "STATUS CODE: ", r.status_code return Response(serializer.data, status=status.HTTP_200_OK) return HttpResponse(status=status.HTTP_403_FORBIDDEN) else: remote_author = request.data.get('author') author_id = request.data.get('author').get('id') friend_id = request.data.get('friend').get('id') local_author = Author.objects.get(uuid=friend_id) if remote_author.get('displayname'): author_name = remote_author.get('displayname') if remote_author.get('displayName'): author_name = remote_author.get('displayName') tempuser = User(username=author_name, password="******") tempuser.save() tempauthor = Author(user=tempuser, uuid=author_id) tempauthor.save() friendRequest = FriendRequest(requester=tempauthor, receiver=local_author) friendRequest.save() serializer = FriendRequestSerializer(friendRequest) return Response(status=status.HTTP_200_OK)