Beispiel #1
0
    def test_comment_delete_handler(self):
        # POST test topic via '/topic/add'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)

        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        params = {"title": title, "content": content, "csrf_token": csrf_token}

        post = self.testapp.post('/topic/add', params)
        self.assertEqual(post.status_int, 302)

        # POST test comment via '/topic/details/<topic_id>'
        content = "This is a new comment. Just for testing purposes."

        params = {"content": content, "csrf_token": csrf_token}

        topic = Topic.query().get()
        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Comment.save_comment(topic_id, content)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()), params)
        self.assertEqual(post.status_int, 302)

        # Delete comment via '/comment/delete/<comment_id>'
        comment = Comment.query().get()
        params = {"csrf_token": csrf_token}

        post = self.testapp.post('/comment/delete/' + str(comment.key.id()), params, {'referer': '/user-comments'})
        self.assertEqual(post.status_int, 302)
        # check if comment.deleted field was set to True
        self.assertEqual(comment.deleted, True)

        post = self.testapp.post('/comment/delete/' + str(comment.key.id()), params, {'referer': '/topic/details/' + str(topic.key.id())})
        self.assertEqual(post.status_int, 302)
Beispiel #2
0
    def test_topic_add_handler(self):
        # GET
        get = self.testapp.get('/topic/add')
        self.assertEqual(get.status_int, 200)

        # POST
        csrf_token = str(uuid.uuid4())  # convert UUID to string
        memcache.add(key=csrf_token, value=True, time=600)

        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        params = {"title": title, "content": content, "csrf_token": csrf_token}

        post = self.testapp.post('/topic/add', params)  # do a POST request
        self.assertEqual(
            post.status_int, 302
        )  # 302 means "redirect" - this is what we do at the end of POST method in TopicAdd handler

        topic = Topic.query().get(
        )  # get the topic create by this text (it's the only one in this fake database)
        self.assertEqual(
            topic.title,
            title)  # check if topic title is the same as we wrote above
        self.assertEqual(topic.content, content)
Beispiel #3
0
    def get(self):
        time_limit = datetime.now() - timedelta(days=1)

        latest_topics = Topic.query(
            Topic.create_time > time_limit).fetch()

        latest_topics_text = ""

        for topic in latest_topics:
            latest_topics_text = ", </br>".join(topic.title)

        subscriptions = SubscriptionLatestTopics.query().fetch()

        subscribers_list = []

        for subscription in subscriptions:
            subscribers_list.append(subscription.user_id)

        if subscribers_list:
            for subscriber in subscribers_list:
                taskqueue.add(url="/task/send-latest-topics-mail",
                              params={
                                  "latest_topics_text": latest_topics_text,
                                  "receiver": subscriber,
                              })
Beispiel #4
0
    def get(self):
        # fetch saves data to topics variable so the next database query
        # doesn't have to be performed if needed
        topics = Topic.query(
            Topic.deleted == False).order(-Topic.create_time).fetch()

        params = {"topics": topics}
        return self.render_template_with_csrf("home.html", params)
Beispiel #5
0
    def get(self):
        time_delete = datetime.now() - timedelta(days=30)

        topics_to_delete = Topic.query(
            Topic.deleted == True,
            Topic.deleted_time != None,
            Topic.deleted_time < time_delete).fetch()
        for topic in topics_to_delete:
            topic.key.delete()
Beispiel #6
0
    def test_topic_details_handler(self):
        # Create test topic
        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        topic = Topic(user_id=os.environ['USER_EMAIL'],
                      title=title,
                      content=content)
        topic.put()

        # GET
        topic = Topic.query().get()
        get = self.testapp.get('/topic/details/' + str(topic.key.id()))
        self.assertEqual(get.status_int, 200)
        self.assertEqual(topic.title, title)

        # POST
        # 1. POST test comment via '/topic/details/<topic_id>'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)
        content = "This is a new comment. Just for testing purposes."

        params = {"content": content, "csrf_token": csrf_token}

        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Comment.save_comment(topic_id, content)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)

        comment = Comment.query().get()
        self.assertEqual(comment.content, content)

        # 2. POST test subscription via '/topic/details/<topic_id>'
        params = {"csrf_token": csrf_token}

        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Subscription.save_comment(topic_id, user_id)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)

        subscription = Subscription.query().get()
        self.assertEqual(subscription.user_id, os.environ['USER_EMAIL'])
Beispiel #7
0
    def test_topic_delete_handler(self):
        # Create test topic
        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        topic = Topic(user_id=os.environ['USER_EMAIL'],
                      title=title,
                      content=content)
        topic.put()

        # Delete test topic via '/topic/delete/<topic_id>'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)
        topic = Topic.query().get()

        params = {"csrf_token": csrf_token}

        post = self.testapp.post('/topic/delete/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)
        # check if topic.deleted field was set to True
        self.assertEqual(topic.deleted, True)
Beispiel #8
0
    def get(self):
        topics = Topic.query(Topic.deleted == False).fetch()

        params = {"topics": topics}

        return self.render_template("main.html", params=params)
Beispiel #9
0
    def get(self):
        topic = Topic.query().fetch()
        user = users.get_current_user()
        params = {"topics": topic, "user": user}

        return self.render_template("main.html", params=params)
Beispiel #10
0
    def get(self):
        topic = Topic.query().fetch()
        params = {"topics": topic}

        return self.render_template("main.html", params=params)