Ejemplo n.º 1
0
    def setUp(self):
        self.c = Client()
        self.author_A = Author.signup("author_A", "pw", "pw")
        self.author_B = Author.signup("author_B", "pw", "pw")

        self.author_A.is_active = True
        self.author_B.is_active = True

        self.posts = [
            Post.objects.create(date=datetime.date.today(),
                                title="a",
                                content="a",
                                author=self.author_A,
                                privacy=Privacy.PUBLIC),
            Post.objects.create(date=datetime.date.today(),
                                title="b",
                                content="b",
                                author=self.author_A,
                                privacy=Privacy.PUBLIC),
            Post.objects.create(date=datetime.date.today(),
                                title="c",
                                content="c",
                                author=self.author_B,
                                privacy=Privacy.PUBLIC)
        ]
Ejemplo n.º 2
0
def profile(request, user_id):
    """
    Local handler for viewing author profiles, the author in question might be local or foreign,
    both should be supported
    """
    stripped_user_id = url_regex.sub("", user_id).rstrip("/")

    if Author.is_uid_local(stripped_user_id):
        # @todo , this template expects a uuid in order to render, it should be able to handle a uid
        invalidate_friends(request.get_host(), stripped_user_id)
        invalidate_friend_requests(stripped_user_id)
        return render(request, 'users/profile.html', {
            'user_id': Author.extract_uuid_from_uid(stripped_user_id),  # uuid
            'user_full_id': stripped_user_id,  # uid

            'post_view_url': reverse('view_post', args=['00000000000000000000000000000000']).replace('00000000000000000000000000000000/', '')
        })

    return render(request, 'users/profile.html', {
        'user_id': Author.extract_uuid_from_uid(stripped_user_id),  # uuid
        'user_full_id': stripped_user_id,  # uid
        'post_view_url': reverse('view_post', args=[stripped_user_id])


    })
Ejemplo n.º 3
0
    def test_delete(self):
        author = Author(username="******",
                        email="*****@*****.**",
                        password="******",
                        first_name="test",
                        last_name="test",
                        is_active=1)
        author.uid = "testserver/author/" + str(author.id)
        author.save()

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author)
        profile = reverse("update_author_profile", args=[author.id])

        response = self.client.post(profile, {
            'first_name': 'test',
            'last_name': 'test',
            'email': '*****@*****.**',
            'bio': '',
            'github': '',
            'display_name': '',
            'delete': True
        },
                                    content_type="application/json")
        self.assertEquals(response.status_code, 200)

        self.assertEquals(
            Author.objects.filter(username='******').exists(), False)
Ejemplo n.º 4
0
    def setUp(self):
        self.author_A = Author.signup("a", "pw", "pw")
        self.author_B = Author.signup("b", "pw", "pw")

        self.post = Post.objects.create(date=datetime.date.today(),
                                        title="a",
                                        content="a",
                                        author=self.author_A,
                                        privacy=Privacy.PUBLIC)
Ejemplo n.º 5
0
    def setUp(self):
        self.author_A = Author.signup("a", "pw", "pw")
        self.author_B = Author.signup("b", "pw", "pw")
        self.author_C = Author.signup("c", "pw", "pw")

        self.author_A.follow(self.author_B)
        self.author_B.follow(self.author_A)

        self.author_C.follow(self.author_A)
Ejemplo n.º 6
0
    def setUp(self):
        self.c = Client()
        self.author_A = Author.signup("author_A", "pw", "pw")
        self.author_B = Author.signup("author_B", "pw", "pw")
        self.author_C = Author.signup("author_C", "pw", "pw")

        self.author_A.follow(self.author_B)
        self.author_B.follow(self.author_A)
        self.author_A.follow(self.author_C)
        self.author_C.follow(self.author_A)
Ejemplo n.º 7
0
    def setUp(self):
        self.test_author = Author(username="******",
                                  email="*****@*****.**",
                                  password="******",
                                  first_name="testing",
                                  last_name="testing",
                                  is_active=0)
        self.test_author.uid = "testserver/author/" + self.test_author.id.hex
        self.test_author.save()
        self.retrieve_author_profile_url = reverse(
            'retrieve_author_profile', args=[self.test_author.id.hex])
        # sample test data: http://service/author/de305d54-75b4-431b-adb2-eb6b9e546013/friends/127.0.0.1:5454/author/ae345d54-75b4-431b-adb2-fb6b9e547891

        self.client = Client()
Ejemplo n.º 8
0
    def test_friends_posts(self):
        author_C = Author.signup("c", "pw", "pw")
        author_D = Author.signup("d", "pw", "pw")
        author_E = Author.signup("e", "pw", "pw")

        author_C.follow(author_D)
        author_D.follow(author_C)
        self.assertTrue(author_C.friends_with(author_D))

        cfs = list(author_C.get_friends())
        self.assertEqual(1, len(cfs))
        self.assertTrue(author_D in cfs)
        dfs = list(author_D.get_friends())
        self.assertEqual(1, len(dfs))
        self.assertTrue(author_C in dfs)

        author_C.follow(author_E)
        author_E.follow(author_C)
        self.assertTrue(author_C.friends_with(author_E))

        post_D = Post.objects.create(date=datetime.date.today(),
                                     title="a",
                                     content="a",
                                     author=author_D)

        post_E = Post.objects.create(date=datetime.date.today(),
                                     title="a",
                                     content="a",
                                     author=author_E)

        post_E_private = Post.objects.create(date=datetime.date.today(),
                                             title="a",
                                             content="a",
                                             author=author_E,
                                             privacy=Privacy.PRIVATE)

        post_D_url = Post.objects.create(date=datetime.date.today(),
                                         title="a",
                                         content="a",
                                         author=author_D,
                                         privacy=Privacy.URL_ONLY)

        fps = list(author_C.friends_posts())
        self.assertEqual(2, len(fps))
        self.assertTrue(post_D in fps)
        self.assertTrue(post_E in fps)
Ejemplo n.º 9
0
    def test_fail_invalid_author_id_posts_visible_to_auth_user(self):
        author1 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="test",
                         last_name="test",
                         host="testserver",
                         is_active=1)
        author1.uid = "testserver/author/" + str(author1.id)
        author1.save()
        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author1)
        view_post_of_author = reverse(
            'retrieve_posts_of_author_id_visible_to_current_auth_user',
            args=[1])
        response = self.client.get(view_post_of_author)

        self.assertEqual(404, response.status_code)
Ejemplo n.º 10
0
 def setUp(self):
     self.c = Client(HTTP_AUTHORIZATION="Basic YXV0aG9yOnB3")
     self.author = Author.signup("author", "pw", "pw")
     u = User.objects.get(username='******')
     u.is_active = True
     u.save()
     self.post = Post.objects.create(date=datetime.date.today(),
                                     title="a",
                                     content="a",
                                     author=self.author,
                                     privacy=Privacy.PUBLIC)
Ejemplo n.º 11
0
    def test_follow(self):
        author_A = Author.signup("a", "pw", "pw")
        author_B = Author.signup("b", "pw", "pw")

        self.assertFalse(author_A.follows(author_B))
        self.assertFalse(author_B.follows(author_A))
        self.assertFalse(author_B.friends_with(author_A))
        self.assertFalse(author_A.friends_with(author_B))

        author_A.follow(author_B)
        self.assertTrue(author_A.follows(author_B))
        self.assertFalse(author_B.follows(author_A))
        self.assertFalse(author_B.friends_with(author_A))
        self.assertFalse(author_A.friends_with(author_B))

        author_B.follow(author_A)
        self.assertTrue(author_A.follows(author_B))
        self.assertTrue(author_B.follows(author_A))
        self.assertTrue(author_B.friends_with(author_A))
        self.assertTrue(author_A.friends_with(author_B))
Ejemplo n.º 12
0
    def test_view_friend_candidate(self):
        # create two test authors
        author = Author(username="******",
                        email="*****@*****.**",
                        password="******",
                        first_name="testing",
                        last_name="testing",
                        is_active=1)
        author.uid = "testserver/author/" + str(author.id)
        author.save()
        author2 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="testing",
                         last_name="testing",
                         is_active=1)
        author2.uid = "testserver/author/" + str(author2.id)
        author2.save()

        view_friend_candidate_url = reverse(
            'view_list_of_available_authors_to_befriend', args=[author.id])

        response = self.client.get(view_friend_candidate_url)
        json_response = response.json()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            json_response["available_authors_to_befriend"][0] == author2.uid)
        # friend author with author2
        new_friend1 = Friend(author_id=author.uid, friend_id=author2.uid)
        new_friend1.save()

        response = self.client.get(view_friend_candidate_url)
        json_response = response.json()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            len(json_response["available_authors_to_befriend"]) == 0)
        # clean up test data
        Friend.objects.filter(author_id=author.uid).filter(
            friend_id=author2.uid).delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()
Ejemplo n.º 13
0
 def setUp(self):
     self.c = Client()
     self.author = Author.signup("author", "pw", "pw")
     self.posts = [
         Post.objects.create(date=datetime.date.today(),
                             title="a",
                             content="a",
                             author=self.author,
                             privacy=Privacy.PUBLIC),
         Post.objects.create(date=datetime.date.today(),
                             title="b",
                             content="b",
                             author=self.author,
                             privacy=Privacy.PUBLIC),
         Post.objects.create(date=datetime.date.today(),
                             title="c",
                             content="c",
                             author=self.author,
                             privacy=Privacy.PUBLIC)
     ]
Ejemplo n.º 14
0
    def viewable_by(self, user):
        if (self.privacy == Privacy.PUBLIC
                or self.privacy == Privacy.URL_ONLY):
            return True

        viewer = Author.from_user(user)

        if viewer is None:
            return False

        if self.author == viewer:
            return True

        elif self.privacy == Privacy.FRIENDS:
            return self.author.friends_with(viewer)
        elif self.privacy == Privacy.FOAF:
            #TODO
            return False

        return False
Ejemplo n.º 15
0
def posts(request):
    if request.method == "POST":
        visibility_table = {
            "PRIVATE": Privacy.PRIVATE,
            #"PUBLIC": Privacy.URL_ONLY,
            "FRIENDS": Privacy.FRIENDS,
            "FOAF": Privacy.FOAF,
            "PUBLIC": Privacy.PUBLIC,
        }
        author = Author.from_user(request.user)
        if author is None:
            return JsonResponse({}, status=401)

        try:
            data = json.loads(request.body)
            title   = data["title"]
            content = data["content"]
            content_type = data["contentType"]
            privacy = data["visibility"] if "visibility" in data else "PUBLIC"
            privacy = visibility_table[privacy]
            if "unlisted" in data and data["unlisted"]:
                privacy = Privacy.URL_ONLY
        except (json.decoder.JSONDecodeError, KeyError):
            return JsonResponse({}, status=400)


        post = Post.objects.create(date=datetime.date.today(),
                                   title=title,
                                   content=content,
                                   author=author,
                                   content_type=content_type,
                                   image=None, #TODO
                                   privacy=privacy)

        return redirect("api_post", post.pk)
    else:
        return render_posts(
            request,
            blog.models.Post.public(),
            api_reverse(request, "api_posts")
        )
Ejemplo n.º 16
0
def post(request, post_id):
    post = get_object_or_404(Post, pk=post_id)

    if request.method == "PUT":
        visibility_table = {
            "PRIVATE": Privacy.PRIVATE,
            #"PUBLIC": Privacy.URL_ONLY,
            "FRIENDS": Privacy.FRIENDS,
            "FOAF": Privacy.FOAF,
            "PUBLIC": Privacy.PUBLIC,
        }
        author = Author.from_user(request.user)
        if author is None or author != post.author:
            return JsonResponse({}, status=401)

        try:
            data = json.loads(request.body)
        except json.decoder.JSONDecodeError:
            return JsonResponse({}, status=400)

        if "title" in data:
            post.title   = data["title"]
        if "content" in data:
            post.content = data["content"]
        if "contentType" in data:
            post.content_type = data["contentType"]
        if "visibility" in data:
            try:
                post.privacy = visibility_table[data["visibility"]]
            except KeyError:
                return JsonResponse({}, status=400)
        if "unlisted" in data and data["unlisted"]:
            post.privacy = Privacy.URL_ONLY

        post.save()

    return JsonResponse(serialize_post(request, post))
Ejemplo n.º 17
0
 def test_normal_signup(self):
     a = Author.signup("b", "x", "x")
     self.assertTrue(isinstance(a, Author))
Ejemplo n.º 18
0
    def test_author_id_posts_visible_to_auth_user(self):
        author1 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="test",
                         last_name="test",
                         host="testserver",
                         is_active=1)
        author1.uid = "testserver/author/" + str(author1.id)
        author1.save()
        author2 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="post",
                         last_name="post",
                         host="testserver",
                         is_active=1)
        author2.uid = "testserver/author/" + str(author2.id)
        author2.save()
        author3 = Author(username="******",
                         email="post_friend@post_friend.com",
                         password="******",
                         first_name="post_friend",
                         last_name="post_friend",
                         host="testserver",
                         is_active=1)
        author3.uid = "testserver/author/" + str(author3.id)
        author3.save()
        friendship = [
            # author1 and author2 are friends
            Friend(author_id=author1.uid, friend_id=author2.uid),
            Friend(author_id=author2.uid, friend_id=author1.uid),
            # author2 and author3 are friends
            Friend(author_id=author2.uid, friend_id=author3.uid),
            Friend(author_id=author3.uid, friend_id=author2.uid),
        ]
        for friends in friendship:
            friends.save()

        # posts created by author2
        author2_friends = Post(author=author2,
                               content="friends post created by author2",
                               visibility=Post.FRIENDS,
                               title='friends post')
        author2_friends.size = 1
        author2_friends.contentType = 'text/plain'
        author2_friends.save()
        author2_public = Post(author=author2,
                              content="public post created by author2",
                              visibility=Post.PUBLIC,
                              title='public post')
        author2_public.size = 1
        author2_public.contentType = 'text/plain'
        author2_public.save()

        author2_private = Post(
            author=author2,
            content="private post created by author2 not visible to test",
            visibility=Post.PRIVATE,
            title='private post')
        author2_private.visibleTo.set([author1.uid])
        author2_private.size = 1
        author2_private.contentType = 'text/plain'
        author2_private.save()

        # post created by author3
        author3_foaf = Post(author=author3,
                            content="fofa post created by author3",
                            visibility=Post.FOAF,
                            title='foaf post')
        author3_foaf.size = 1
        author3_foaf.contentType = 'text/plain'
        author3_foaf.save()
        author3_private = Post(author=author3,
                               content="private post created by author3",
                               visibility=Post.PRIVATE,
                               title='privage post')
        author3_private.size = 1
        author3_private.contentType = 'text/plain'
        author3_private.save()

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author1)

        viewable_post_for_author3 = [author3_foaf]
        viewable_post_for_author2 = [
            author2_private, author2_public, author2_friends
        ]

        post_id_author3 = []
        post_id_author2 = []

        for post in viewable_post_for_author3:
            post_id_author3.append(str(post.id))

        for post in viewable_post_for_author2:
            post_id_author2.append(str(post.id))

        view_post_of_author2 = reverse(
            'retrieve_posts_of_author_id_visible_to_current_auth_user',
            args=[author2.id])
        view_post_of_author3 = reverse(
            'retrieve_posts_of_author_id_visible_to_current_auth_user',
            args=[author3.id])

        response = self.client.get(view_post_of_author2)

        self.assertEqual(200, response.status_code)

        # getting json
        json_data = response.json()

        self.assertEquals(json_data['query'], 'posts')
        self.assertEquals(json_data['count'], len(viewable_post_for_author2))
        self.assertEquals(json_data['size'], 10)

        returned_post_id = []
        for each_post in json_data['posts']:
            returned_post_id.append(each_post['id'])

        self.assertEqual(post_id_author2, returned_post_id)

        response = self.client.get(view_post_of_author3)

        self.assertEqual(200, response.status_code)

        # getting json
        json_data = response.json()

        self.assertEquals(json_data['query'], 'posts')
        self.assertEquals(json_data['count'], len(viewable_post_for_author3))
        self.assertEquals(json_data['size'], 10)

        returned_post_id = []
        for each_post in json_data['posts']:
            returned_post_id.append(each_post['id'])

        self.assertEqual(post_id_author3, returned_post_id)

        Post.objects.filter(author=author1).delete()
        Post.objects.filter(author=author2).delete()
        Post.objects.filter(author=author3).delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()
Ejemplo n.º 19
0
 def setUp(self):
     self.c = Client()
     self.author_A = Author.signup("author_A", "pw", "pw")
     self.author_B = Author.signup("author_B", "pw", "pw")
Ejemplo n.º 20
0
class TestViews(TestCase):
    def setUp(self):
        self.test_author = Author(username="******",
                                  email="*****@*****.**",
                                  password="******",
                                  first_name="testing",
                                  last_name="testing",
                                  is_active=0)
        self.test_author.uid = "testserver/author/" + self.test_author.id.hex
        self.test_author.save()
        self.retrieve_author_profile_url = reverse(
            'retrieve_author_profile', args=[self.test_author.id.hex])
        # sample test data: http://service/author/de305d54-75b4-431b-adb2-eb6b9e546013/friends/127.0.0.1:5454/author/ae345d54-75b4-431b-adb2-fb6b9e547891

        self.client = Client()

    # test forbidden methods for service/author/<str:author_id>/

    def test_retrieve_author_profile_method_not_allowed(self):
        # create test_author
        # 405 except GET
        response = self.client.post(self.retrieve_author_profile_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.delete(self.retrieve_author_profile_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.put(self.retrieve_author_profile_url)
        self.assertTrue(response.status_code == 405)

    # test for retrieving inactive author service/author/<str:author_id>/

    def test_retrieve_inactive_author_profile(self):

        # user should not be retrievalble if not active
        response = self.client.get(self.retrieve_author_profile_url)
        self.assertTrue(response.status_code == 404)

    # test for retrieving active author service/author/<str:author_id>/
    def test_retrieve_active_author_profile(self):

        self.test_author.is_active = 1
        self.test_author.save()
        response = self.client.get(self.retrieve_author_profile_url)

        self.assertTrue(response.status_code == 200)
        json_response = response.json()

        self.assertEquals(json_response['id'], self.test_author.uid)
        self.assertEquals(json_response['firstName'], "testing")
        self.assertEquals(json_response['firstName'], "testing")
        self.assertEquals(json_response['lastName'], "testing")
        self.assertEquals(json_response['email'], "*****@*****.**")

    # test for <str:author1_id>/friends/<path:author2_id>/
    def test_friend_checking_and_retrieval(self):
        check_if_friends_url = reverse(
            'check_if_two_authors_are_friends',
            kwargs={
                'author1_id':
                'de305d54-75b4-431b-adb2-eb6b9e546013',
                'author2_id':
                '127.0.0.1:5454/author/ae345d54-75b4-431b-adb2-fb6b9e547891'
            })
        response = self.client.get(check_if_friends_url)
        self.assertTrue(response.status_code == 200)
        json_response = response.json()
        self.assertFalse(json_response['friends'])
        self.assertEquals(json_response['query'], "friends")
        self.assertEquals(
            json_response['authors'][0],
            "testserver/author/de305d54-75b4-431b-adb2-eb6b9e546013")
        self.assertEquals(
            json_response['authors'][1],
            "127.0.0.1:5454/author/ae345d54-75b4-431b-adb2-fb6b9e547891")

    # test forbidden methods for <str:author1_id>/friends/<path:author2_id>/

    def test_friend_checking_and_retrieval_method_not_allowed(self):
        check_if_friends_url = reverse(
            'check_if_two_authors_are_friends',
            kwargs={
                'author1_id':
                'de305d54-75b4-431b-adb2-eb6b9e546013',
                'author2_id':
                '127.0.0.1:5454/author/ae345d54-75b4-431b-adb2-fb6b9e547891'
            })
        # 405 except GET
        response = self.client.post(check_if_friends_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.put(check_if_friends_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.delete(check_if_friends_url)
        self.assertTrue(response.status_code == 405)

    # test GET author/<path:author_id>/friends/

    def test_friend_checking_and_retrieval_GET(self):
        friend_checking_and_retrieval_url = reverse(
            'friend_checking_and_retrieval_of_author_id',
            args=[self.test_author.uid])
        response = self.client.get(friend_checking_and_retrieval_url)
        json_response = response.json()

        self.assertTrue(response.status_code == 200)
        # test response data entry
        self.assertEquals(json_response["query"], "friends")
        self.assertFalse(json_response["authors"])
        test_friend = Friend(author_id=self.test_author.uid,
                             friend_id="test_friend")
        test_friend.save()

        response = self.client.get(friend_checking_and_retrieval_url)
        json_response = response.json()
        self.assertTrue(json_response["authors"][0] == "test_friend")
        # clean up test data
        Friend.objects.filter(author_id=self.test_author.uid).filter(
            friend_id="test_friend").delete()

    # test POST author/<path:author_id>/friends/
    def test_friend_checking_and_retrieval_POST(self):
        friend_checking_and_retrieval_url = reverse(
            'friend_checking_and_retrieval_of_author_id',
            args=[self.test_author.uid])

        test_friend = Friend(author_id=self.test_author.uid,
                             friend_id="test_friend")
        test_friend.save()
        post_data = {
            "query": "friends",
            "authors": ["test_friend", "not_a_friend"]
        }
        response = self.client.post(friend_checking_and_retrieval_url,
                                    data=post_data,
                                    content_type="application/json")
        json_response = response.json()
        self.assertTrue(response.status_code == 200)
        self.assertEquals(json_response["query"], "friends")
        self.assertEquals(json_response["author"], self.test_author.uid)
        self.assertEquals(len(json_response["authors"]), 1)
        # clean up test data
        Friend.objects.filter(author_id=self.test_author.uid).filter(
            friend_id="test_friend").delete()

    # test forbidden method author/<path:author_id>/friends/

    def test_friend_checking_and_retrieval_not_allowed_method(self):
        friend_checking_and_retrieval_url = reverse(
            'friend_checking_and_retrieval_of_author_id',
            args=[self.test_author.uid])

        response = self.client.put(friend_checking_and_retrieval_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.delete(friend_checking_and_retrieval_url)
        self.assertTrue(response.status_code == 405)

    # test forbidden methods for http://service/author/unfriend

    def test_unfriend_method_not_allowed(self):
        unfriend_url = reverse("unfriend")
        response = self.client.get(unfriend_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.put(unfriend_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.delete(unfriend_url)
        self.assertTrue(response.status_code == 405)

    # test  http://service/author/unfriend

    def test_unfriend(self):
        unfriend_url = reverse("unfriend")
        new_friend = Friend(author_id="A", friend_id="B")
        new_friend.save()

        post_data = {
            "author_id": new_friend.author_id,
            "friend_id": new_friend.friend_id
        }
        response = self.client.post(unfriend_url,
                                    data=post_data,
                                    content_type="application/json")

        self.assertTrue(response.status_code, 200)

        self.assertFalse(
            Friend.objects.filter(author_id="A", friend_id="B").exists())
        Friend.objects.filter(author_id="A").filter(friend_id="B").delete()

    # http://service/<str:author_id>/addfriend/
    def test_view_friend_candidate_method_not_allowed(self):
        view_friend_candidate_url = reverse(
            'view_list_of_available_authors_to_befriend',
            args=[self.test_author.id])
        response = self.client.post(view_friend_candidate_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.put(view_friend_candidate_url)
        self.assertTrue(response.status_code == 405)
        response = self.client.delete(view_friend_candidate_url)
        self.assertTrue(response.status_code == 405)

    # http://service/<str:author_id>/addfriend/
    def test_view_friend_candidate(self):
        # create two test authors
        author = Author(username="******",
                        email="*****@*****.**",
                        password="******",
                        first_name="testing",
                        last_name="testing",
                        is_active=1)
        author.uid = "testserver/author/" + str(author.id)
        author.save()
        author2 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="testing",
                         last_name="testing",
                         is_active=1)
        author2.uid = "testserver/author/" + str(author2.id)
        author2.save()

        view_friend_candidate_url = reverse(
            'view_list_of_available_authors_to_befriend', args=[author.id])

        response = self.client.get(view_friend_candidate_url)
        json_response = response.json()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            json_response["available_authors_to_befriend"][0] == author2.uid)
        # friend author with author2
        new_friend1 = Friend(author_id=author.uid, friend_id=author2.uid)
        new_friend1.save()

        response = self.client.get(view_friend_candidate_url)
        json_response = response.json()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            len(json_response["available_authors_to_befriend"]) == 0)
        # clean up test data
        Friend.objects.filter(author_id=author.uid).filter(
            friend_id=author2.uid).delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()

    # http://service/author/posts
    def test_posts_visible_to_auth_user(self):
        author1 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="test",
                         last_name="test",
                         host="testserver",
                         is_active=1)
        author1.uid = "testserver/author/" + str(author1.id)
        author1.save()
        author2 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="post",
                         last_name="post",
                         host="testserver",
                         is_active=1)
        author2.uid = "testserver/author/" + str(author2.id)
        author2.save()
        author3 = Author(username="******",
                         email="post_friend@post_friend.com",
                         password="******",
                         first_name="post_friend",
                         last_name="post_friend",
                         host="testserver",
                         is_active=1)
        author3.uid = "testserver/author/" + str(author3.id)
        author3.save()
        friendship = [
            # author1 and author2 are friends
            Friend(author_id=author1.uid, friend_id=author2.uid),
            Friend(author_id=author2.uid, friend_id=author1.uid),
            # author2 and author3 are friends
            Friend(author_id=author2.uid, friend_id=author3.uid),
            Friend(author_id=author3.uid, friend_id=author2.uid),
        ]

        for friends in friendship:
            friends.save()

        author1_private = Post(author=author1,
                               content="private post created by test",
                               visibility=Post.PRIVATE,
                               title='private post')
        author1_private.size = 1
        author1_private.contentType = 'text/plain'
        author1_private.save()

        # posts created by author2
        author2_friends = Post(author=author2,
                               content="friends post created by author2",
                               visibility=Post.FRIENDS,
                               title='friends post')
        author2_friends.size = 1
        author2_friends.contentType = 'text/plain'
        author2_friends.save()

        author2_private = Post(
            author=author2,
            content="private post created by author2 not visible to test",
            visibility=Post.PRIVATE,
            title='private post not visible')
        author2_private.size = 1
        author2_private.contentType = 'text/plain'
        author2_private.save()

        # post created by author3
        author3_foaf = Post(author=author3,
                            content="fofa post created by author3",
                            visibility=Post.FOAF,
                            title='foaf post')
        author3_foaf.size = 1
        author3_foaf.contentType = 'text/plain'
        author3_foaf.save()

        viewable_post = [author3_foaf, author2_friends, author1_private]
        post_id = []
        for post in viewable_post:
            post_id.append(str(post.id))

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author1)

        response = self.client.get('/author/posts/')

        self.assertEqual(200, response.status_code)

        json_data = response.json()

        self.assertEquals(json_data['query'], 'posts')
        self.assertEquals(json_data['count'], len(viewable_post))
        self.assertEquals(json_data['size'], 10)

        returned_post_id = []
        for each_post in json_data['posts']:
            returned_post_id.append(each_post['id'])

        self.assertEqual(post_id, returned_post_id)
        Post.objects.filter(author=author1).delete()
        Post.objects.filter(author=author2).delete()
        Post.objects.filter(author=author3).delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()

    def test_fail_invalid_author_id_posts_visible_to_auth_user(self):
        author1 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="test",
                         last_name="test",
                         host="testserver",
                         is_active=1)
        author1.uid = "testserver/author/" + str(author1.id)
        author1.save()
        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author1)
        view_post_of_author = reverse(
            'retrieve_posts_of_author_id_visible_to_current_auth_user',
            args=[1])
        response = self.client.get(view_post_of_author)

        self.assertEqual(404, response.status_code)

    # http://service/author/{author_id}/posts
    def test_author_id_posts_visible_to_auth_user(self):
        author1 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="test",
                         last_name="test",
                         host="testserver",
                         is_active=1)
        author1.uid = "testserver/author/" + str(author1.id)
        author1.save()
        author2 = Author(username="******",
                         email="*****@*****.**",
                         password="******",
                         first_name="post",
                         last_name="post",
                         host="testserver",
                         is_active=1)
        author2.uid = "testserver/author/" + str(author2.id)
        author2.save()
        author3 = Author(username="******",
                         email="post_friend@post_friend.com",
                         password="******",
                         first_name="post_friend",
                         last_name="post_friend",
                         host="testserver",
                         is_active=1)
        author3.uid = "testserver/author/" + str(author3.id)
        author3.save()
        friendship = [
            # author1 and author2 are friends
            Friend(author_id=author1.uid, friend_id=author2.uid),
            Friend(author_id=author2.uid, friend_id=author1.uid),
            # author2 and author3 are friends
            Friend(author_id=author2.uid, friend_id=author3.uid),
            Friend(author_id=author3.uid, friend_id=author2.uid),
        ]
        for friends in friendship:
            friends.save()

        # posts created by author2
        author2_friends = Post(author=author2,
                               content="friends post created by author2",
                               visibility=Post.FRIENDS,
                               title='friends post')
        author2_friends.size = 1
        author2_friends.contentType = 'text/plain'
        author2_friends.save()
        author2_public = Post(author=author2,
                              content="public post created by author2",
                              visibility=Post.PUBLIC,
                              title='public post')
        author2_public.size = 1
        author2_public.contentType = 'text/plain'
        author2_public.save()

        author2_private = Post(
            author=author2,
            content="private post created by author2 not visible to test",
            visibility=Post.PRIVATE,
            title='private post')
        author2_private.visibleTo.set([author1.uid])
        author2_private.size = 1
        author2_private.contentType = 'text/plain'
        author2_private.save()

        # post created by author3
        author3_foaf = Post(author=author3,
                            content="fofa post created by author3",
                            visibility=Post.FOAF,
                            title='foaf post')
        author3_foaf.size = 1
        author3_foaf.contentType = 'text/plain'
        author3_foaf.save()
        author3_private = Post(author=author3,
                               content="private post created by author3",
                               visibility=Post.PRIVATE,
                               title='privage post')
        author3_private.size = 1
        author3_private.contentType = 'text/plain'
        author3_private.save()

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author1)

        viewable_post_for_author3 = [author3_foaf]
        viewable_post_for_author2 = [
            author2_private, author2_public, author2_friends
        ]

        post_id_author3 = []
        post_id_author2 = []

        for post in viewable_post_for_author3:
            post_id_author3.append(str(post.id))

        for post in viewable_post_for_author2:
            post_id_author2.append(str(post.id))

        view_post_of_author2 = reverse(
            'retrieve_posts_of_author_id_visible_to_current_auth_user',
            args=[author2.id])
        view_post_of_author3 = reverse(
            'retrieve_posts_of_author_id_visible_to_current_auth_user',
            args=[author3.id])

        response = self.client.get(view_post_of_author2)

        self.assertEqual(200, response.status_code)

        # getting json
        json_data = response.json()

        self.assertEquals(json_data['query'], 'posts')
        self.assertEquals(json_data['count'], len(viewable_post_for_author2))
        self.assertEquals(json_data['size'], 10)

        returned_post_id = []
        for each_post in json_data['posts']:
            returned_post_id.append(each_post['id'])

        self.assertEqual(post_id_author2, returned_post_id)

        response = self.client.get(view_post_of_author3)

        self.assertEqual(200, response.status_code)

        # getting json
        json_data = response.json()

        self.assertEquals(json_data['query'], 'posts')
        self.assertEquals(json_data['count'], len(viewable_post_for_author3))
        self.assertEquals(json_data['size'], 10)

        returned_post_id = []
        for each_post in json_data['posts']:
            returned_post_id.append(each_post['id'])

        self.assertEqual(post_id_author3, returned_post_id)

        Post.objects.filter(author=author1).delete()
        Post.objects.filter(author=author2).delete()
        Post.objects.filter(author=author3).delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()
        Author.objects.filter(username="******").delete()

    # < str: author_id > / update
    def test_profile_update(self):
        author = Author(username="******",
                        email="*****@*****.**",
                        password="******",
                        first_name="test",
                        last_name="test",
                        is_active=1)
        author.uid = "testserver/author/" + str(author.id)
        author.save()

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author)
        profile = reverse("update_author_profile", args=[author.id])

        response = self.client.post(profile, {
            'first_name': 'test',
            'last_name': 'test',
            'email': '*****@*****.**',
            'bio': 'updated',
            'github': '',
            'display_name': '',
            'delete': False
        },
                                    content_type="application/json")
        author.refresh_from_db()
        self.assertEquals(response.status_code, 200)

        self.assertEquals(author.first_name, 'test')
        self.assertEquals(author.bio, 'updated')

        Author.objects.filter(username='******').delete()

    def test_delete(self):
        author = Author(username="******",
                        email="*****@*****.**",
                        password="******",
                        first_name="test",
                        last_name="test",
                        is_active=1)
        author.uid = "testserver/author/" + str(author.id)
        author.save()

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author)
        profile = reverse("update_author_profile", args=[author.id])

        response = self.client.post(profile, {
            'first_name': 'test',
            'last_name': 'test',
            'email': '*****@*****.**',
            'bio': '',
            'github': '',
            'display_name': '',
            'delete': True
        },
                                    content_type="application/json")
        self.assertEquals(response.status_code, 200)

        self.assertEquals(
            Author.objects.filter(username='******').exists(), False)

    def test_fail_delete(self):
        author = Author(username="******",
                        email="*****@*****.**",
                        password="******",
                        first_name="test",
                        last_name="test",
                        is_active=1)
        author.uid = "testserver/author/" + str(author.id)
        author.save()

        self.client = Client(HTTP_ACCEPT="application/json")
        self.client.force_login(author)
        profile = reverse("update_author_profile", args=[author.id])
        response = self.client.post(profile, {
            'first_name': 'test',
            'last_name': 'test',
            'email': '*****@*****.**',
            'bio': '',
            'github': '',
            'delete': False
        },
                                    content_type="application/json")
        self.assertEquals(response.status_code, 422)

    def tearDown(self):
        Author.objects.filter(username="******").delete()
Ejemplo n.º 21
0
    def setUp(self):
        self.fixture_authors = [
            # Authors
            Author(username="******",
                   email="*****@*****.**",
                   password="******",
                   first_name="A",
                   last_name="A"),
            Author(username="******",
                   email="*****@*****.**",
                   password="******",
                   first_name="B",
                   last_name="B"),
            Author(username="******",
                   email="*****@*****.**",
                   password="******",
                   first_name="C",
                   last_name="C"),

            # Super User
            Author(username="******",
                   email="*****@*****.**",
                   password="******",
                   first_name="admin",
                   last_name="."),
        ]
        for author in self.fixture_authors:
            author.is_active = 1
            author.save()

        self.fixture_posts_public = [
            Post(title='A post 1 public',
                 content="A1 content",
                 author=self.fixture_authors[0]),
            Post(title='A post 2 public',
                 content="A2 content",
                 author=self.fixture_authors[0]),
            Post(title='A post 3 public',
                 content="A3 content",
                 author=self.fixture_authors[0]),
            Post(title='B post 1 public',
                 content="B1 content",
                 author=self.fixture_authors[1]),
            Post(title='B post 2 public',
                 content="B2 content",
                 author=self.fixture_authors[1]),
            Post(title='B post 3 public',
                 content="B3 content",
                 author=self.fixture_authors[1]),
            Post(title='C post 1 public',
                 content="C1 content",
                 author=self.fixture_authors[2]),
            Post(title='C post 2 public',
                 content="C2 content",
                 author=self.fixture_authors[2]),
            Post(title='C post 3 public',
                 content="C3 content",
                 author=self.fixture_authors[2]),
        ]
        for post in self.fixture_posts_public:
            post.size = 0
            post.visibility = Post.PUBLIC
            post.contentType = "text/plain"
        print(self.fixture_posts_public)
        for post in self.fixture_posts_public:
            post.save()

        self.fixture_posts_other = [
            Post(author=self.fixture_authors[0],
                 content="A foaf content",
                 visibility=Post.FOAF,
                 title='A post foaf'),
            Post(author=self.fixture_authors[0],
                 content="A friend content",
                 visibility=Post.FRIENDS,
                 title='A post friends'),
            Post(author=self.fixture_authors[0],
                 content="A private content",
                 visibility=Post.PRIVATE,
                 title='A post private'),
            Post(author=self.fixture_authors[0],
                 content="A server content",
                 visibility=Post.SERVERONLY,
                 title='A post server'),
            Post(author=self.fixture_authors[1],
                 content="B foaf content",
                 visibility=Post.FOAF,
                 title='B post foaf'),
            Post(author=self.fixture_authors[1],
                 content="B friend content",
                 visibility=Post.FRIENDS,
                 title='B post friends'),
            Post(author=self.fixture_authors[1],
                 content="B private content",
                 visibility=Post.PRIVATE,
                 title='B post private'),
            Post(author=self.fixture_authors[1],
                 content="B server content",
                 visibility=Post.SERVERONLY,
                 title='B post server'),
            Post(author=self.fixture_authors[2],
                 content="C foaf content",
                 visibility=Post.FOAF,
                 title='C post foaf'),
            Post(author=self.fixture_authors[2],
                 content="C friend content",
                 visibility=Post.FRIENDS,
                 title='C post friends'),
            Post(author=self.fixture_authors[2],
                 content="C private content",
                 visibility=Post.PRIVATE,
                 title='C post private'),
            Post(author=self.fixture_authors[2],
                 content="C server content",
                 visibility=Post.SERVERONLY,
                 title='C post server'),
        ]
        for post in self.fixture_posts_other:
            post.size = 0
            post.contentType = 'text/plain'
            post.save()

        # Because we are testing the API document, we should NOT use reverse to get the URIs
        # Instead we should hardcode the URI's such that we ensure we are always spec compliant

        self.url_public_posts = "/posts"
        self.url_get_post = lambda post_id: f"/posts/{post_id}"
        self.url_get_visible_posts = "author/posts"
        self.client = Client(HTTP_ACCEPT="application/json")
Ejemplo n.º 22
0
 def setUp(self):
     self.author_A = Author.signup("a", "pw", "pw")
Ejemplo n.º 23
0
 def test_username_taken(self):
     try:
         Author.signup("a", "x", "x")
         self.fail()
     except Author.UserNameTaken:
         pass
Ejemplo n.º 24
0
 def test_passwords_dont_match(self):
     try:
         Author.signup("c", "x", "y")
         self.fail()
     except Author.PasswordsDontMatch:
         pass
Ejemplo n.º 25
0
    def test_from_user(self):
        self.assertEqual(self.author_A, Author.from_user(self.author_A.user))

        user = User.objects.create_user(username="******", password="******")
        self.assertIs(None, Author.from_user(user))