Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1 

2import os 

3import sys 

4import pytest 

5from testfixtures import log_capture 

6sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../") 

7from sdc_etl_libs.sdc_credentials.sdc_endpoint_credentials import SDCEndpointCredentials 

8from sdc_etl_libs.sdc_data_schema.schema_exceptions import InvalidSchemaCredentialsType 

9 

10 

11def test_get_credentials__bad_credentials_type(mocker): 

12 """ 

13 Ensure schema_exceptions.InvalidCredentialsType is raised if an invalid credentials type is given. 

14 """ 

15 

16 credentials_schema = { 

17 "type": "totally_not_a_real_creds_type", 

18 "opts": { 

19 "name": "not-real-secret" 

20 } 

21 } 

22 

23 with pytest.raises(InvalidSchemaCredentialsType): 

24 SDCEndpointCredentials.get_credentials(credentials_schema) 

25 

26 

27def test_get_credentials_for_aws_secrets__happy_path(mocker): 

28 """ 

29 Ensure secrets are returned for type "aws_secrets" when all information is given. 

30 NOTE: Test for error when returning secrets from AWS covered by AWSHelpers.get_secrets. 

31 """ 

32 

33 aws_creds_mock = { 

34 "username": "sdc.data", 

35 "password": "supercool" 

36 } 

37 

38 mocker.patch('sdc_etl_libs.aws_helpers.aws_helpers.AWSHelpers.get_secrets', 

39 return_value=aws_creds_mock, autospec=True) 

40 

41 credentials_schema = { 

42 "type": "aws_secrets", 

43 "opts": { 

44 "name": "not-real-secret" 

45 } 

46 } 

47 

48 result = SDCEndpointCredentials.get_credentials(credentials_schema) 

49 

50 assert result == aws_creds_mock 

51 

52 

53def test_get_credentials_for_environment_variables__happy_path(): 

54 """ 

55 Ensure secrets are returned for type "environment_variables" when all information is given and envirnomental 

56 variable exists. 

57 """ 

58 

59 credentials_schema = { 

60 "type": "environment_variables", 

61 "opts": { 

62 "variables": { 

63 "username": "sdc_etl_libs_test_NOT_REAL_USER_ifQXcesHcF", 

64 "password": "sdc_etl_libs_test_NOT_REAL_PWD_N8tNi29jtc", 

65 } 

66 } 

67 } 

68 

69 try: 

70 del os.environ["sdc_etl_libs_test_NOT_REAL_USER_ifQXcesHcF"] 

71 except: 

72 pass 

73 try: 

74 del os.environ["sdc_etl_libs_test_NOT_REAL_PWD_N8tNi29jtc"] 

75 except: 

76 pass 

77 os.environ["sdc_etl_libs_test_NOT_REAL_USER_ifQXcesHcF"] = "rad.dude" 

78 os.environ["sdc_etl_libs_test_NOT_REAL_PWD_N8tNi29jtc"] = "awesome.password" 

79 

80 result = SDCEndpointCredentials.get_credentials(credentials_schema) 

81 

82 excepted_result = { 

83 "username": "rad.dude", 

84 "password": "awesome.password", 

85 } 

86 

87 assert result == excepted_result 

88 

89 

90#@log_capture() 

91def test_get_credentials_for_environment_variables__var_missing(): 

92 """ 

93 Ensure that if a env var is missing, it is not included in the resulting creds dictionary 

94 """ 

95 

96 #TODO: There appears to be an issue in SDCEndpointCredentials.get_credentials() wherein 

97 # a missing environmental var does not raise the logging.info when called via os.environ["..."] 

98 # Need to figure out why and update this test according to check log_capture for the message 

99 

100 credentials_schema = { 

101 "type": "environment_variables", 

102 "opts": { 

103 "variables": { 

104 "username": "sdc_etl_libs_test_NOT_REAL_USER_ifAccesHc2", 

105 "password": "sdc_etl_libs_test_NOT_REAL_PWD_N8tNi29jtc", 

106 } 

107 } 

108 } 

109 

110 try: 

111 del os.environ["sdc_etl_libs_test_NOT_REAL_USER_ifAccesHc2"] 

112 except: 

113 pass 

114 try: 

115 del os.environ["sdc_etl_libs_test_NOT_REAL_PWD_N8tNi29jtc"] 

116 except: 

117 pass 

118 os.environ["sdc_etl_libs_test_NOT_REAL_USER_ifAccesHc2"] = "rad.dude" 

119 

120 excepted_result = { 

121 "username": "rad.dude" 

122 } 

123 

124 result = SDCEndpointCredentials.get_credentials(credentials_schema) 

125 assert result == excepted_result 

126 

127 #capture.check_present( 

128 # ('root', 'INFO', 'No Environment Variable found for : password') 

129 #) 

130