예제 #1
0
 def test_save_no_redirects(self):
     """Raise a ValidationError if save is run with no data to save."""
     rdf = RedirectsFile('/tmp/foo.txt')
     with pytest.raises(ValueError):
         rdf.save('/tmp/bar.txt')
     with pytest.raises(ValueError):
         rdf.save()
예제 #2
0
 def test_add_redirect_old_path_in_use(self):
     """Raise ValueError if a redirect already exists from an old path."""
     rd1 = Redirect('/old/path', '/new/one', 302)
     rd2 = Redirect('/old/path', '/new/two', 302)
     rdf = RedirectsFile('/tmp/redirects.json')
     rdf.add_redirect(rd1)
     with pytest.raises(ValueError):
         rdf.add_redirect(rd2)
예제 #3
0
 def test_exists(self, mock_exists):
     """Return True if file specified by self.file_name exists."""
     rdf = RedirectsFile('/tmp/foo.json')
     mock_exists.return_value = True
     assert rdf.exists()
     mock_exists.return_value = False
     assert not rdf.exists()
     mock_exists.assert_called_with('/tmp/foo.json')
예제 #4
0
 def test_add_redirect_prevents_chains(self):
     """If adding a redirect would create a chain, edit old redirect."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/two', '/three', 302)
     rdf = RedirectsFile('/tmp/redirects.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     for rd in rdf.redirects:
         assert rd.new_path == '/three'
예제 #5
0
 def test_save(self):
     """Save redirects to specified file or self.file_name."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/three', '/four', 302)
     rd3 = Redirect('/five', '/six', 302)
     rdf = RedirectsFile('/tmp/foo.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     rdf.add_redirect(rd3)
     m = mock.mock_open()
     with mock.patch('builtins.open', m, create=True):
         m.return_value = StringIO()
         rdf.save('/tmp/bar.txt')
         m.assert_called_with('/tmp/bar.txt', 'w', encoding='utf-8')
     m = mock.mock_open()
     with mock.patch('builtins.open', m, create=True):
         m.return_value = StringIO()
         rdf.save()
         m.assert_called_with('/tmp/foo.json', 'w', encoding='utf-8')
예제 #6
0
 def test_remove_redirect(self):
     """Remove a redirect from self.redirects."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/three', '/four', 302)
     rd3 = Redirect('/five', '/six', 302)
     rdf = RedirectsFile('/tmp/foo.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     rdf.add_redirect(rd3)
     assert len(rdf.redirects) == 3
     assert rd2 in rdf.redirects
     rdf.remove_redirect(rd2)
     assert len(rdf.redirects) == 2
     assert rd2 not in rdf.redirects
예제 #7
0
    def test_add_redirect_new_path_redirected_from(self):
        """Raise ValueError if a redirect already exists from a new path.

        This should also prevent circular redirects.
        """
        rd1 = Redirect('/path/one', '/path/two', 302)
        rd2 = Redirect('/path/three', '/path/one', 302)
        rd3 = Redirect('/path/two', '/path/one', 302)
        rdf = RedirectsFile('/tmp/foo.json')
        rdf.add_redirect(rd1)
        with pytest.raises(ValueError):
            rdf.add_redirect(rd2)
        with pytest.raises(ValueError):
            rdf.add_redirect(rd3)
예제 #8
0
 def test_load_file(self):
     """Populate self.redirects with JSON data from a file-like object."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/three', '/four', 302)
     rd3 = Redirect('/five', '/six', 302)
     rdf = RedirectsFile('/tmp/foo.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     rdf.add_redirect(rd3)
     json_file = StringIO()
     json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects]))
     json_file.seek(0)
     rdf2 = RedirectsFile('/tmp/foo.json')
     rdf2.load_file(json_file)
     assert rdf.redirects == rdf2.redirects
     json_file = StringIO()
     json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects]))
     json_file.seek(0)
예제 #9
0
 def test_save_file(self):
     """Save redirects to a JSON string in a file or file-like object."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/three', '/four', 302)
     rd3 = Redirect('/five', '/six', 302)
     rdf = RedirectsFile('/tmp/foo.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     rdf.add_redirect(rd3)
     json_file = StringIO()
     json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects]))
     json_file.seek(0)
     json_ofile = StringIO()
     rdf.save_file(json_ofile)
     json_ofile.seek(0)
     assert json_file.read() == json_ofile.read()
예제 #10
0
 def test_get_redirect_with_old_path(self):
     """Return redirect with given old_path if it exists, None if not."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/three', '/four', 302)
     rdf = RedirectsFile('/tmp/foo.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     retrd = rdf.get_redirect_with_old_path('/three')
     assert retrd == rd2
     retrd = rdf.get_redirect_with_old_path('/two')
     assert retrd is None
     retrd = rdf.get_redirect_with_old_path('/four')
     assert retrd is None
예제 #11
0
 def test_load(self):
     """Load redirects from specified file or self.file_name."""
     rd1 = Redirect('/one', '/two', 302)
     rd2 = Redirect('/three', '/four', 302)
     rd3 = Redirect('/five', '/six', 302)
     rdf = RedirectsFile('/tmp/foo.json')
     rdf.add_redirect(rd1)
     rdf.add_redirect(rd2)
     rdf.add_redirect(rd3)
     json_file = StringIO()
     json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects]))
     json_file.seek(0)
     rdf2 = RedirectsFile('/tmp/foo.json')
     m = mock.mock_open()
     with mock.patch('builtins.open', m, create=True):
         m.return_value = json_file
         rdf2.load('/tmp/bar.json')
         m.assert_called_with('/tmp/bar.json', 'r', encoding='utf-8')
     json_file = StringIO()
     json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects]))
     json_file.seek(0)
     rdf2 = RedirectsFile('/tmp/foo.json')
     m = mock.mock_open()
     with mock.patch('builtins.open', m, create=True):
         m.return_value = json_file
         rdf2.load()
         m.assert_called_with('/tmp/foo.json', 'r', encoding='utf-8')
예제 #12
0
 def test_add_redirect_wrong_type(self):
     """Raise TypeError if given data that is not a Redirect object."""
     rdf = RedirectsFile('/tmp/foo.json')
     with pytest.raises(TypeError):
         rdf.add_redirect('/old/path')
예제 #13
0
 def test_repr(self):
     """Return a string representing the redirects file."""
     rdf = RedirectsFile('/tmp/redirects.json')
     assert rdf.__repr__() == '<RedirectsFile \'/tmp/redirects.json\'>'