Coverage for tests/test_api.py: 77%

1 import warnings

2

3 import edwh

4 import pytest

5

6 from src.edwh_uptime_plugin.uptimerobot import UptimeRobot, UptimeRobotException

7 from src.edwh_uptime_plugin.uptimerobot import uptime_robot as illegal

8

9# if the name registered in the account does not match, exit immediately because that's not good!!!

10 REQUIRED_NAME = edwh.get_env_value("REQUIRED_NAME", "EDWH-pytest")

11

12# global safety measure:

13 pytest.allowed_apikey = None

14

15

16 def new_safe_uptimerobot():

17 instance = UptimeRobot()

18

19 account = instance.get_account_details()

20 if account.get("firstname", "") != REQUIRED_NAME: 20 ↛ 21line 20 didn't jump to line 21, because the condition on line 20 was never true

21 warnings.warn(

22 f"!!! INVALID API KEY, EXIT NOW TO PREVENT UPDATING ACTUAL DATA !!! "

23 f"({account.get('firstname', '')} != {REQUIRED_NAME})",

24 category=UserWarning,

25 )

26 exit(1)

27

28 pytest.allowed_apikey = instance._api_key

29 return instance

30

31

32@pytest.fixture

33 def uptime_robot():

34 """

35 Get a guaranteed-safe UptimeRobot instance (= test account only) to prevent horrors

36 """

37 illegal._api_key = "<DON'T USE ME>"

38

39 if pytest.allowed_apikey: 39 ↛ 40line 39 didn't jump to line 40, because the condition on line 39 was never true

40 instance = UptimeRobot()

41 instance._api_key = pytest.allowed_apikey

42 yield instance

43

44 return

45

46 # else: first validate the API key:

47 yield new_safe_uptimerobot()

48

49

50 def test_000_prevent_horrors(uptime_robot):

51 # check AGAIN just to be safe

52 assert uptime_robot.get_account_details().get("firstname") == REQUIRED_NAME

53

54

55 def test_001_prevent_horrors():

56 # check the original instance to make sure we're not allowed to mess anything up:

57 with pytest.raises(UptimeRobotException):

58 assert not illegal.get_account_details()

59

60

61# continue testing on proper test instance: