test_login.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Tests for login redirects"""
  2. import requests
  3. from tornado.httputil import url_concat
  4. from notebook.tests.launchnotebook import NotebookTestBase
  5. class LoginTest(NotebookTestBase):
  6. def login(self, next):
  7. first = requests.get(self.base_url() + "login")
  8. first.raise_for_status()
  9. resp = requests.post(
  10. url_concat(
  11. self.base_url() + "login",
  12. {'next': next},
  13. ),
  14. allow_redirects=False,
  15. data={
  16. "password": self.token,
  17. "_xsrf": first.cookies.get("_xsrf", ""),
  18. },
  19. cookies=first.cookies,
  20. )
  21. resp.raise_for_status()
  22. return resp.headers['Location']
  23. def test_next_bad(self):
  24. for bad_next in (
  25. "//some-host",
  26. "//host" + self.url_prefix + "tree",
  27. "https://google.com",
  28. "/absolute/not/base_url",
  29. "///jupyter.org",
  30. "/\\some-host",
  31. ):
  32. url = self.login(next=bad_next)
  33. self.assertEqual(url, self.url_prefix)
  34. assert url
  35. def test_next_ok(self):
  36. for next_path in (
  37. "tree/",
  38. self.base_url() + "has/host",
  39. "notebooks/notebook.ipynb",
  40. "tree//something",
  41. ):
  42. if "://" in next_path:
  43. expected = next_path
  44. else:
  45. expected = self.url_prefix + next_path
  46. actual = self.login(next=expected)
  47. self.assertEqual(actual, expected)