Example #1
0
 def test_paste_has_correct_history(self):
     """
     Upload a paste, update it and check that the paste history is displayed correctly
     """
     create_test_account(self)
     login_test_account(self)
     
     test_user = User.objects.get(username="******")
     
     paste = Paste()
     char_id = paste.add_paste(user=test_user,
                               text="This is a test paste number one.",
                               title="Tested paste")
     
     self.client.post(reverse("pastes:edit_paste", kwargs={"char_id": char_id}),
                                                          {"title": "New paste title",
                                                           "text": "This is the new text",
                                                           "syntax_highlighting": "text",
                                                           "expiration": "never",
                                                           "visibility": "public",
                                                           "note": "Testing update note"},
                             follow=True)
     
     response = self.client.get(reverse("pastes:paste_history", kwargs={"char_id": char_id}))
     
     self.assertContains(response, "Testing update note")
Example #2
0
    def test_user_can_edit_paste(self):
        """
        Upload a paste while logged in and edit it
        """
        create_test_account(self)
        login_test_account(self)

        test_user = User.objects.get(username="******")

        paste = Paste()
        char_id = paste.add_paste(user=test_user,
                                  text="This is a test paste number one.",
                                  title="Tested paste")

        self.assertEquals(len(char_id), 8)

        # Edit the paste
        response = self.client.post(reverse("pastes:edit_paste",
                                            kwargs={"char_id": char_id}), {
                                                "title": "New paste title",
                                                "text": "This is the new text",
                                                "syntax_highlighting": "text",
                                                "expiration": "never",
                                                "visibility": "public"
                                            },
                                    follow=True)

        self.assertContains(response, "New paste title")
        self.assertContains(response, "This is the new text")

        self.assertNotContains(response, "This is a test paste number one.")
Example #3
0
 def test_user_can_edit_paste(self):
     """
     Upload a paste while logged in and edit it
     """
     create_test_account(self)
     login_test_account(self)
     
     test_user = User.objects.get(username="******")
     
     paste = Paste()
     char_id = paste.add_paste(user=test_user,
                               text="This is a test paste number one.",
                               title="Tested paste")
     
     self.assertEquals(len(char_id), 8)
     
     # Edit the paste
     response = self.client.post(reverse("pastes:edit_paste", kwargs={"char_id": char_id}),
                                                              {"title": "New paste title",
                                                               "text": "This is the new text",
                                                               "syntax_highlighting": "text",
                                                               "expiration": "never",
                                                               "visibility": "public"},
                                 follow=True)
     
     self.assertContains(response, "New paste title")
     self.assertContains(response, "This is the new text")
     
     self.assertNotContains(response, "This is a test paste number one.")
Example #4
0
    def test_paste_has_correct_history(self):
        """
        Upload a paste, update it and check that the paste history is displayed correctly
        """
        create_test_account(self)
        login_test_account(self)

        test_user = User.objects.get(username="******")

        paste = Paste()
        char_id = paste.add_paste(user=test_user,
                                  text="This is a test paste number one.",
                                  title="Tested paste")

        self.client.post(reverse("pastes:edit_paste",
                                 kwargs={"char_id": char_id}), {
                                     "title": "New paste title",
                                     "text": "This is the new text",
                                     "syntax_highlighting": "text",
                                     "expiration": "never",
                                     "visibility": "public",
                                     "note": "Testing update note"
                                 },
                         follow=True)

        response = self.client.get(
            reverse("pastes:paste_history", kwargs={"char_id": char_id}))

        self.assertContains(response, "Testing update note")
Example #5
0
 def test_paste_versions_can_be_viewed(self):
     """
     Upload a paste, update it twice and check that all versions can be viewed individually
     """
     ""
     create_test_account(self)
     login_test_account(self)
     
     test_user = User.objects.get(username="******")
     
     paste = Paste()
     char_id = paste.add_paste(user=test_user,
                               text="This is the version one.",
                               title="Tested paste")
     
     self.client.post(reverse("pastes:edit_paste", kwargs={"char_id": char_id}),
                                                          {"title": "Second version title",
                                                           "text": "This is the version two",
                                                           "syntax_highlighting": "python",
                                                           "expiration": "never",
                                                           "visibility": "public",
                                                           "note": "Update two"},
                             follow=True)
     
     self.client.post(reverse("pastes:edit_paste", kwargs={"char_id": char_id}),
                                                          {"title": "Third version title",
                                                           "text": "This is the version three",
                                                           "syntax_highlighting": "php",
                                                           "expiration": "never",
                                                           "visibility": "public",
                                                           "note": "Testing update note"},
                             follow=True)
     
     # Test that the normal link has the newest version
     response = self.client.get(reverse("show_paste", kwargs={"char_id": char_id}))
     
     self.assertContains(response, "PHP")
     self.assertContains(response, "Third version title")
     
     # Test the first version
     response = self.client.get(reverse("show_paste", kwargs={"char_id": char_id,
                                                              "version": 1}))
     
     self.assertContains(response, "Text only")
     self.assertContains(response, "Tested paste")
     
     # Test the second version
     response = self.client.get(reverse("show_paste", kwargs={"char_id": char_id,
                                                              "version": 2}))
     
     self.assertContains(response, "Python")
     self.assertContains(response, "Second version title")
     
     # Test the third version
     response = self.client.get(reverse("show_paste", kwargs={"char_id": char_id,
                                                              "version": 3}))
     
     self.assertContains(response, "PHP")
     self.assertContains(response, "Third version title")
Example #6
0
def upload_test_paste(test_case, username="******"):
    """
    Upload a test paste
    """
    paste = Paste()

    test_user = User.objects.get(username=username)

    return paste.add_paste(user=test_user,
                           text="This is the test paste.",
                           title="Test paste")
Example #7
0
def upload_test_paste(test_case, username="******"):
    """
    Upload a test paste
    """
    paste = Paste()
    
    test_user = User.objects.get(username=username)
    
    return paste.add_paste(user=test_user,
                           text="This is the test paste.",
                           title="Test paste")
Example #8
0
def upload_test_paste(test_case,
                      username="******",
                      text="This is the test paste."):
    """
    Upload a test paste
    """
    paste = Paste()

    if username != None:
        test_user = User.objects.get(username=username)
    else:
        test_user = None

    return paste.add_paste(user=test_user, text=text, title="Test paste")
Example #9
0
def upload_test_paste(test_case, username="******", text="This is the test paste."):
    """
    Upload a test paste
    """
    paste = Paste()
    
    if username != None:
        test_user = User.objects.get(username=username)
    else:
        test_user = None
        
    return paste.add_paste(user=test_user,
                           text=text,
                           title="Test paste")
Example #10
0
 def test_raw_paste_displayed_correctly(self):
     """
     Create a paste and view it in raw format
     """
     text = """This is a raw paste.<b></b>>TEST TEST TEST     TEST"""
     
     paste = Paste()
     char_id = paste.add_paste(text)
     
     response = self.client.get(reverse("raw_paste", kwargs={"char_id": char_id}))
     
     # The response should contain the next text and nothing else
     self.assertContains(response, text)
     self.assertNotContains(response, "Untitled")
Example #11
0
    def test_raw_paste_displayed_correctly(self):
        """
        Create a paste and view it in raw format
        """
        text = """This is a raw paste.<b></b>>TEST TEST TEST     TEST"""

        paste = Paste()
        char_id = paste.add_paste(text)

        response = self.client.get(
            reverse("raw_paste", kwargs={"char_id": char_id}))

        # The response should contain the next text and nothing else
        self.assertContains(response, text)
        self.assertNotContains(response, "Untitled")
Example #12
0
def _create_paste(request):
    """ HTTP POST: Creates a paste, and then returns the UID for the paste """
    if request.method == 'POST':
        if request.POST.get('content') is None:
            err = {'error': 'Paste content cannot be empty'}
            return JsonResponse(err, status=422)

        paste = Paste()
        paste.content = request.POST.get('content')

        # If a title is defined by the user, use the title provided.
        # Otherwise, it will fall back to "Untitled Paste"
        if request.POST.get('title') is not None:
            paste.title = request.POST.get('title')

        # If a password is provided, encrypt the password and then store
        if request.POST.get('password') is not None:
            paste.password = hashers.make_password(request.POST.get('password'))

        # Get the IP address from the request, using the helper function.
        paste.ip_addr = _get_client_ip(request)

        paste.save()

        msg = {
            'message': 'The paste has been successfully posted',
            'uid': paste.uid,
        }
        response = JsonResponse(msg, status=200)
        response['Location'] = str(paste.uid)  # Add Paste UID to Location header
        return response
    else:
        err = {'error': 'Bad Request'}
        return JsonResponse(err, status=400)
Example #13
0
    def test_encrypted_paste_versions_shown_correctly(self):
        """
        Upload an encrypted paste and then update that paste to not have encryption
        Check that both versions are displayed correctly
        """
        create_test_account(self)
        login_test_account(self)

        test_user = User.objects.get(username="******")

        paste = Paste()
        char_id = paste.add_paste(user=test_user,
                                  text="This is the version one.",
                                  title="Tested paste",
                                  encrypted=True)

        self.client.post(reverse("pastes:edit_paste",
                                 kwargs={"char_id": char_id}), {
                                     "title": "Second version title",
                                     "text": "This is the version two",
                                     "syntax_highlighting": "python",
                                     "expiration": "never",
                                     "visibility": "public",
                                     "encrypted": False,
                                     "note": "Update two"
                                 },
                         follow=True)

        # Test the first encrypted version
        response = self.client.get(
            reverse("show_paste", kwargs={
                "char_id": char_id,
                "version": 1
            }))

        self.assertContains(response, "This paste is encrypted")

        # Test the second unencrypted version
        response = self.client.get(
            reverse("show_paste", kwargs={
                "char_id": char_id,
                "version": 2
            }))

        self.assertNotContains(response, "This paste is encrypted")
Example #14
0
def home(request):
    """
    Display the index page with the form to submit a paste, as well as the most recent
    pastes
    """
    paste_form = SubmitPasteForm(request.POST or None, request=request)

    latest_pastes = cache.get("home_latest_pastes")

    if latest_pastes == None:
        latest_pastes = Paste.objects.get_pastes(include_expired=False,
                                                 include_hidden=False,
                                                 count=15)
        cache.set("home_latest_pastes", latest_pastes, 5)

    languages = highlighting.settings.LANGUAGES

    if paste_form.is_valid():
        paste_data = paste_form.cleaned_data

        user = None
        if request.user.is_authenticated():
            user = request.user

        paste = Paste()

        char_id = paste.add_paste(title=paste_data["title"],
                                  user=user,
                                  text=paste_data["text"],
                                  expiration=paste_data["expiration"],
                                  visibility=paste_data["visibility"],
                                  format=paste_data["syntax_highlighting"],
                                  encrypted=paste_data["encrypted"])

        Limiter.increase_action_count(request, Limiter.PASTE_UPLOAD)

        # Redirect to the newly created paste
        return redirect("show_paste", char_id=char_id)

    return render(request, "home/home.html", {
        "form": paste_form,
        "latest_pastes": latest_pastes,
        "languages": languages
    })
Example #15
0
def random_paste(request):
    """
    Redirect to a random paste
    """
    char_id = Paste.get_random_char_id()
    
    if char_id:
        return redirect("show_paste", char_id=char_id)
    else:
        return redirect("home:home")
Example #16
0
def random_paste(request):
    """
    Redirect to a random paste
    """
    char_id = Paste.get_random_char_id()

    if char_id:
        return redirect("show_paste", char_id=char_id)
    else:
        return redirect("home:home")
Example #17
0
 def test_expiring_paste_expires_correctly(self):
     """
     Upload a paste with an expiration time
     """
     with freeze_time("2015-01-01 12:00:00"):
         paste = Paste()
         char_id = paste.add_paste(text="This is a test paste",
                                   title="Expiring paste title",
                                   expiration=Paste.ONE_HOUR)
         
         response = self.client.post(reverse("show_paste", kwargs={"char_id": char_id}))
         
         self.assertContains(response, "Expiring paste title")
         self.assertNotContains(response, "The paste you tried to view has expired")
         
     with freeze_time("2015-01-01 13:00:01"):
         response = self.client.post(reverse("show_paste", kwargs={"char_id": char_id}))
         
         self.assertContains(response, "The paste you tried to view has expired", status_code=404)
Example #18
0
def home(request):
    """
    Display the index page with the form to submit a paste, as well as the most recent
    pastes
    """
    paste_form = SubmitPasteForm(request.POST or None, request=request)
    
    latest_pastes = cache.get("home_latest_pastes")
    
    if latest_pastes == None:
        latest_pastes = Paste.objects.get_pastes(include_expired=False, include_hidden=False,
                                                 count=15)
        cache.set("home_latest_pastes", latest_pastes, 5)
    
    languages = highlighting.settings.LANGUAGES
    
    if paste_form.is_valid():
        paste_data = paste_form.cleaned_data
        
        user = None
        if request.user.is_authenticated():
            user = request.user
        
        paste = Paste()
        
        char_id = paste.add_paste(title=paste_data["title"],
                                  user=user,
                                  text=paste_data["text"],
                                  expiration=paste_data["expiration"],
                                  visibility=paste_data["visibility"],
                                  format=paste_data["syntax_highlighting"],
                                  encrypted=paste_data["encrypted"])
        
        Limiter.increase_action_count(request, Limiter.PASTE_UPLOAD)
        
        # Redirect to the newly created paste
        return redirect("show_paste", char_id=char_id)
    
    return render(request, "home/home.html", {"form": paste_form,
                                              "latest_pastes": latest_pastes,
                                              "languages": languages })
Example #19
0
 def test_encrypted_paste_versions_shown_correctly(self):
     """
     Upload an encrypted paste and then update that paste to not have encryption
     Check that both versions are displayed correctly
     """
     create_test_account(self)
     login_test_account(self)
     
     test_user = User.objects.get(username="******")
     
     paste = Paste()
     char_id = paste.add_paste(user=test_user,
                               text="This is the version one.",
                               title="Tested paste",
                               encrypted=True)
     
     self.client.post(reverse("pastes:edit_paste", kwargs={"char_id": char_id}),
                                                          {"title": "Second version title",
                                                           "text": "This is the version two",
                                                           "syntax_highlighting": "python",
                                                           "expiration": "never",
                                                           "visibility": "public",
                                                           "encrypted": False,
                                                           "note": "Update two"},
                             follow=True)
     
     # Test the first encrypted version
     response = self.client.get(reverse("show_paste", kwargs={"char_id": char_id,
                                                              "version": 1}))
     
     self.assertContains(response, "This paste is encrypted")
     
     # Test the second unencrypted version
     response = self.client.get(reverse("show_paste", kwargs={"char_id": char_id,
                                                              "version": 2}))
     
     self.assertNotContains(response, "This paste is encrypted")
Example #20
0
    def test_expiring_paste_expires_correctly(self):
        """
        Upload a paste with an expiration time
        """
        with freeze_time("2015-01-01 12:00:00"):
            paste = Paste()
            char_id = paste.add_paste(text="This is a test paste",
                                      title="Expiring paste title",
                                      expiration=Paste.ONE_HOUR)

            response = self.client.post(
                reverse("show_paste", kwargs={"char_id": char_id}))

            self.assertContains(response, "Expiring paste title")
            self.assertNotContains(response,
                                   "The paste you tried to view has expired")

        with freeze_time("2015-01-01 13:00:01"):
            response = self.client.post(
                reverse("show_paste", kwargs={"char_id": char_id}))

            self.assertContains(response,
                                "The paste you tried to view has expired",
                                status_code=404)
Example #21
0
def _create_paste(request):
    """ HTTP POST: Creates a paste, and then returns the UID for the paste """
    if request.method == 'POST':
        if request.POST.get('content') is None:
            err = {'error': 'Paste content cannot be empty'}
            return JsonResponse(err, status=422)

        paste = Paste()
        paste.content = request.POST.get('content')

        # If a title is defined by the user, use the title provided.
        # Otherwise, it will fall back to "Untitled Paste"
        if request.POST.get('title') is not None:
            paste.title = request.POST.get('title')

        # If a password is provided, encrypt the password and then store
        if request.POST.get('password') is not None:
            paste.password = hashers.make_password(
                request.POST.get('password'))

        # Get the IP address from the request, using the helper function.
        paste.ip_addr = _get_client_ip(request)

        paste.save()

        msg = {
            'message': 'The paste has been successfully posted',
            'uid': paste.uid,
        }
        response = JsonResponse(msg, status=200)
        response['Location'] = str(
            paste.uid)  # Add Paste UID to Location header
        return response
    else:
        err = {'error': 'Bad Request'}
        return JsonResponse(err, status=400)
Example #22
0
    def test_paste_versions_can_be_viewed(self):
        """
        Upload a paste, update it twice and check that all versions can be viewed individually
        """
        ""
        create_test_account(self)
        login_test_account(self)

        test_user = User.objects.get(username="******")

        paste = Paste()
        char_id = paste.add_paste(user=test_user,
                                  text="This is the version one.",
                                  title="Tested paste")

        self.client.post(reverse("pastes:edit_paste",
                                 kwargs={"char_id": char_id}), {
                                     "title": "Second version title",
                                     "text": "This is the version two",
                                     "syntax_highlighting": "python",
                                     "expiration": "never",
                                     "visibility": "public",
                                     "note": "Update two"
                                 },
                         follow=True)

        self.client.post(reverse("pastes:edit_paste",
                                 kwargs={"char_id": char_id}), {
                                     "title": "Third version title",
                                     "text": "This is the version three",
                                     "syntax_highlighting": "php",
                                     "expiration": "never",
                                     "visibility": "public",
                                     "note": "Testing update note"
                                 },
                         follow=True)

        # Test that the normal link has the newest version
        response = self.client.get(
            reverse("show_paste", kwargs={"char_id": char_id}))

        self.assertContains(response, "PHP")
        self.assertContains(response, "Third version title")

        # Test the first version
        response = self.client.get(
            reverse("show_paste", kwargs={
                "char_id": char_id,
                "version": 1
            }))

        self.assertContains(response, "Text only")
        self.assertContains(response, "Tested paste")

        # Test the second version
        response = self.client.get(
            reverse("show_paste", kwargs={
                "char_id": char_id,
                "version": 2
            }))

        self.assertContains(response, "Python")
        self.assertContains(response, "Second version title")

        # Test the third version
        response = self.client.get(
            reverse("show_paste", kwargs={
                "char_id": char_id,
                "version": 3
            }))

        self.assertContains(response, "PHP")
        self.assertContains(response, "Third version title")