Coverage for tests/test_session.py: 100%

1 import pytest

2 import requests_mock

3 from tyora.session import MoocfiCsesSession as Session

4

5 test_cookies = {"cookie_a": "value_a", "cookie_b": "value_b"}

6

7

8@pytest.fixture

9 def mock_session() -> Session:

10 return Session(

11 username="test_user@test.com",

12 password="test_password",

13 base_url="https://example.com",

14 cookies=test_cookies,

15 )

16

17

18 def test_login_successful(mock_session: Session) -> None:

19 # Mocking the HTTP response for successful login

20 with requests_mock.Mocker() as m:

21 m.get(

22 "https://example.com/list",

23 text=open("tests/test_data/session_logged_in.html").read(),

24 )

25 mock_session.login()

26 print(mock_session.get("https://example.com/list").text)

27 assert mock_session.is_logged_in

28

29

30 def test_login_failed(mock_session: Session) -> None:

31 # Mocking the HTTP response for failed login

32 with requests_mock.Mocker() as m:

33 m.get(

34 "https://example.com/list",

35 text=open("tests/test_data/session_logged_out.html").read(),

36 )

37 m.get(

38 "https://example.com/login/oauth-redirect?site=mooc.fi",

39 text=open("tests/test_data/tmcmoocfi-oauth-redirect.html").read(),

40 )

41 m.post(

42 "https://example.com/sessions",

43 text=open("tests/test_data/tmcmoocfi-sessions.html").read(),

44 )

45 with pytest.raises(ValueError):

46 mock_session.login()

47

48

49 def test_loading_cookies(mock_session: Session) -> None:

50 assert mock_session.cookies.get_dict() == test_cookies