|
3 | 3 | package workflow |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "strings" |
6 | 7 | "testing" |
7 | 8 | ) |
8 | 9 |
|
@@ -602,3 +603,96 @@ func TestPermissions_AllRead(t *testing.T) { |
602 | 603 | }) |
603 | 604 | } |
604 | 605 | } |
| 606 | + |
| 607 | +func TestFilterJobLevelPermissions(t *testing.T) { |
| 608 | + tests := []struct { |
| 609 | + name string |
| 610 | + input string |
| 611 | + expectEmpty bool |
| 612 | + contains []string |
| 613 | + excludes []string |
| 614 | + }{ |
| 615 | + { |
| 616 | + name: "empty input returns empty", |
| 617 | + input: "", |
| 618 | + expectEmpty: true, |
| 619 | + contains: []string{}, |
| 620 | + excludes: []string{}, |
| 621 | + }, |
| 622 | + { |
| 623 | + name: "standard permissions are preserved", |
| 624 | + input: "permissions:\n contents: read\n issues: write", |
| 625 | + contains: []string{ |
| 626 | + "permissions:", |
| 627 | + " contents: read", |
| 628 | + " issues: write", |
| 629 | + }, |
| 630 | + excludes: []string{}, |
| 631 | + }, |
| 632 | + { |
| 633 | + name: "vulnerability-alerts is filtered out", |
| 634 | + input: "permissions:\n contents: read\n pull-requests: read\n security-events: read\n vulnerability-alerts: read", |
| 635 | + contains: []string{ |
| 636 | + "permissions:", |
| 637 | + " contents: read", |
| 638 | + " pull-requests: read", |
| 639 | + " security-events: read", |
| 640 | + }, |
| 641 | + excludes: []string{"vulnerability-alerts"}, |
| 642 | + }, |
| 643 | + { |
| 644 | + name: "multiple GitHub App-only scopes are filtered out", |
| 645 | + input: "permissions:\n contents: read\n issues: write\n administration: read\n members: read\n vulnerability-alerts: read", |
| 646 | + contains: []string{ |
| 647 | + "permissions:", |
| 648 | + " contents: read", |
| 649 | + " issues: write", |
| 650 | + }, |
| 651 | + excludes: []string{"administration", "members", "vulnerability-alerts"}, |
| 652 | + }, |
| 653 | + { |
| 654 | + name: "only GitHub App-only scopes returns empty string", |
| 655 | + input: "permissions:\n vulnerability-alerts: read\n members: read", |
| 656 | + expectEmpty: true, |
| 657 | + contains: []string{}, |
| 658 | + excludes: []string{"vulnerability-alerts", "members"}, |
| 659 | + }, |
| 660 | + { |
| 661 | + name: "shorthand read-all is preserved unchanged", |
| 662 | + input: "permissions: read-all", |
| 663 | + contains: []string{"permissions: read-all"}, |
| 664 | + excludes: []string{}, |
| 665 | + }, |
| 666 | + { |
| 667 | + name: "shorthand write-all is preserved unchanged", |
| 668 | + input: "permissions: write-all", |
| 669 | + contains: []string{"permissions: write-all"}, |
| 670 | + excludes: []string{}, |
| 671 | + }, |
| 672 | + { |
| 673 | + name: "shorthand none is preserved unchanged", |
| 674 | + input: "permissions: none", |
| 675 | + contains: []string{"permissions: none"}, |
| 676 | + excludes: []string{}, |
| 677 | + }, |
| 678 | + } |
| 679 | + |
| 680 | + for _, tt := range tests { |
| 681 | + t.Run(tt.name, func(t *testing.T) { |
| 682 | + result := filterJobLevelPermissions(tt.input) |
| 683 | + if tt.expectEmpty && result != "" { |
| 684 | + t.Errorf("filterJobLevelPermissions() should return empty string, but got:\n%q", result) |
| 685 | + } |
| 686 | + for _, expected := range tt.contains { |
| 687 | + if !strings.Contains(result, expected) { |
| 688 | + t.Errorf("filterJobLevelPermissions() should contain %q, but got:\n%q", expected, result) |
| 689 | + } |
| 690 | + } |
| 691 | + for _, excluded := range tt.excludes { |
| 692 | + if strings.Contains(result, excluded) { |
| 693 | + t.Errorf("filterJobLevelPermissions() should NOT contain %q, but got:\n%q", excluded, result) |
| 694 | + } |
| 695 | + } |
| 696 | + }) |
| 697 | + } |
| 698 | +} |
0 commit comments