diff --git a/unit-tests/files/csv-import-test.csv b/unit-tests/files/csv-import-test.csv new file mode 100644 index 0000000..a47f3e6 --- /dev/null +++ b/unit-tests/files/csv-import-test.csv @@ -0,0 +1,5 @@ +category,subcategory,groupname,manager,member,member,viewer +test-automation,csv-test,csv-test-group1,groupmanager@yoda.test,researcher@yoda.test,datamanager@yoda.test,viewer@yoda.test +test-automation,csv-test,csv-test-group2,groupmanager@yoda.test,researcher@yoda.test,datamanager@yoda.test,viewer@yoda.test +test-automation,csv-test,csv-test-group3,groupmanager@yoda.test,researcher@yoda.test,datamanager@yoda.test,viewer@yoda.test +test-automation,csv-test,csv-test-group4,groupmanager@yoda.test,researcher@yoda.test,datamanager@yoda.test,viewer@yoda.test diff --git a/unit-tests/files/header-with-suffixes.csv b/unit-tests/files/header-with-suffixes.csv new file mode 100644 index 0000000..a1962d4 --- /dev/null +++ b/unit-tests/files/header-with-suffixes.csv @@ -0,0 +1,3 @@ +category,subcategory,groupname,manager:alice,member:bob,member:charlie,viewer:dan +test-automation,csv-test,csv-test-group1,groupmanager@yoda.test,researcher@yoda.test,datamanager@yoda.test,viewer@yoda.test +test-automation,csv-test,csv-test-group2,groupmanager@yoda.test,researcher@yoda.test,datamanager@yoda.test,viewer@yoda.test diff --git a/unit-tests/test_importgroups.py b/unit-tests/test_importgroups.py index 33bed5f..730e463 100644 --- a/unit-tests/test_importgroups.py +++ b/unit-tests/test_importgroups.py @@ -31,7 +31,7 @@ def test_fully_filled_csv_line_1_9(self): "manager": ["m.manager@yoda.dev"], "member": ["p.member@yoda.dev"], "viewer": ["m.viewer@yoda.dev"], - "expiration_date": ["2025-01-01"], + "expiration_date": ["2030-01-01"], "schema_id": ["default-3"], } expected = ( @@ -42,7 +42,34 @@ def test_fully_filled_csv_line_1_9(self): ["p.member@yoda.dev"], ["m.viewer@yoda.dev"], "default-3", - "2025-01-01", + "2030-01-01", + ) + result, error_msg = _process_csv_line(d, args, "1.9") + self.assertTupleEqual(expected, result) + self.assertIsNone(error_msg) + + def test_fully_filled_csv_line_1_9_with_suffixes(self): + # Confirm support the old csv header format still (with ":nicknameofuser") + args = {"offline_check": True} + d = { + "category": ["test-automation"], + "subcategory": ["initial"], + "groupname": ["groupteama"], + "manager:alice": ["m.manager@yoda.dev"], + "member:bob": ["p.member@yoda.dev"], + "viewer:eve": ["m.viewer@yoda.dev"], + "expiration_date": ["2030-01-01"], + "schema_id": ["default-3"], + } + expected = ( + "test-automation", + "initial", + "research-groupteama", + ["m.manager@yoda.dev"], + ["p.member@yoda.dev"], + ["m.viewer@yoda.dev"], + "default-3", + "2030-01-01", ) result, error_msg = _process_csv_line(d, args, "1.9") self.assertTupleEqual(expected, result) @@ -129,6 +156,19 @@ def test_error_fields_1_8(self): self.assertIsNone(result) self.assertIn("1.9", error_msg) + def test_parse_csv(self): + args = {"offline_check": True} + parse_csv_file("files/csv-import-test.csv", args, "1.9") + + @patch('sys.stderr', new_callable=StringIO) + def test_parse_csv_with_header_suffixes(self, mock_stderr): + args = {"offline_check": True} + parse_csv_file("files/header-with-suffixes.csv", args, "1.8") + + # This header format not supported in 1.9 and higher + with self.assertRaises(SystemExit): + parse_csv_file("files/header-with-suffixes.csv", args, "1.9") + @patch('sys.stderr', new_callable=StringIO) def test_parse_invalid_csv_file(self, mock_stderr): # csv that has an unlabeled header diff --git a/yclienttools/importgroups.py b/yclienttools/importgroups.py index de34018..f419dd9 100644 --- a/yclienttools/importgroups.py +++ b/yclienttools/importgroups.py @@ -41,10 +41,21 @@ def parse_csv_file(input_file, args, yoda_version): # Check that all header names are valid possible_labels = _get_csv_possible_labels(yoda_version) + labels_with_optional_suffix = ['viewer', 'member', 'manager'] for label in header: if label not in possible_labels: - _exit_with_error( - 'CSV header contains unknown field "{}"'.format(label)) + found_match = False + for opt_label in labels_with_optional_suffix: + if label.startswith('{}:'.format(opt_label)): + found_match = True + + if found_match and yoda_version not in ('1.7', '1.8'): + _exit_with_error( + 'This script does not support headers with suffixes in ' + 'Yoda version 1.9 and higher. Field with suffix: "{}"'.format(label)) + elif not found_match: + _exit_with_error( + 'CSV header contains unknown field "{}"'.format(label)) duplicate_columns = _get_duplicate_columns(header, yoda_version) if (len(duplicate_columns) > 0): @@ -146,11 +157,11 @@ def _process_csv_line(line, args, yoda_version): if not is_valid: return None, '"{}" is not a valid username.'.format(item_list[i]) - if column_name.lower() == 'manager': + if column_name.lower() == 'manager' or column_name.lower().startswith('manager:'): managers = item_list - elif column_name.lower() == 'member': + elif column_name.lower() == 'member' or column_name.lower().startswith('member:'): members = item_list - elif column_name.lower() == 'viewer': + elif column_name.lower() == 'viewer' or column_name.lower().startswith('viewer:'): viewers = item_list # perform additional data validations