|
| 1 | +package owners |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +type mockConfigFileReader struct { |
| 10 | + files map[string]string |
| 11 | +} |
| 12 | + |
| 13 | +func (m *mockConfigFileReader) ReadFile(path string) ([]byte, error) { |
| 14 | + content, ok := m.files[path] |
| 15 | + if !ok { |
| 16 | + return nil, fmt.Errorf("file not found: %s", path) |
| 17 | + } |
| 18 | + return []byte(content), nil |
| 19 | +} |
| 20 | + |
| 21 | +func (m *mockConfigFileReader) PathExists(path string) bool { |
| 22 | + _, ok := m.files[path] |
| 23 | + return ok |
| 24 | +} |
| 25 | + |
| 26 | +func TestReadConfigFromGitRef(t *testing.T) { |
| 27 | + // Simulate reading config from a git ref |
| 28 | + mockReader := &mockConfigFileReader{ |
| 29 | + files: map[string]string{ |
| 30 | + "test/repo/codeowners.toml": ` |
| 31 | +max_reviews = 3 |
| 32 | +min_reviews = 2 |
| 33 | +unskippable_reviewers = ["@admin"] |
| 34 | +
|
| 35 | +[enforcement] |
| 36 | +approval = true |
| 37 | +fail_check = false |
| 38 | +`, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + config, err := ReadConfig("test/repo", mockReader) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("unexpected error: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + if config.MaxReviews == nil || *config.MaxReviews != 3 { |
| 48 | + t.Errorf("expected max_reviews = 3, got %v", config.MaxReviews) |
| 49 | + } |
| 50 | + |
| 51 | + if config.MinReviews == nil || *config.MinReviews != 2 { |
| 52 | + t.Errorf("expected min_reviews = 2, got %v", config.MinReviews) |
| 53 | + } |
| 54 | + |
| 55 | + if len(config.UnskippableReviewers) != 1 || config.UnskippableReviewers[0] != "@admin" { |
| 56 | + t.Errorf("expected unskippable_reviewers = [@admin], got %v", config.UnskippableReviewers) |
| 57 | + } |
| 58 | + |
| 59 | + if !config.Enforcement.Approval { |
| 60 | + t.Error("expected enforcement.approval = true") |
| 61 | + } |
| 62 | + |
| 63 | + if config.Enforcement.FailCheck { |
| 64 | + t.Error("expected enforcement.fail_check = false") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestReadConfigFromGitRefNotFound(t *testing.T) { |
| 69 | + // Simulate config file not existing in git ref |
| 70 | + mockReader := &mockConfigFileReader{ |
| 71 | + files: map[string]string{}, |
| 72 | + } |
| 73 | + |
| 74 | + config, err := ReadConfig("test/repo", mockReader) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("unexpected error: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Should return default config |
| 80 | + if config.MaxReviews != nil { |
| 81 | + t.Error("expected nil max_reviews for default config") |
| 82 | + } |
| 83 | + |
| 84 | + if config.MinReviews != nil { |
| 85 | + t.Error("expected nil min_reviews for default config") |
| 86 | + } |
| 87 | + |
| 88 | + if config.Enforcement.Approval { |
| 89 | + t.Error("expected enforcement.approval = false for default config") |
| 90 | + } |
| 91 | + |
| 92 | + if !config.Enforcement.FailCheck { |
| 93 | + t.Error("expected enforcement.fail_check = true for default config") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestReadConfigFromGitRefInvalidToml(t *testing.T) { |
| 98 | + // Simulate invalid TOML content |
| 99 | + mockReader := &mockConfigFileReader{ |
| 100 | + files: map[string]string{ |
| 101 | + "test/repo/codeowners.toml": "invalid toml [[[", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + _, err := ReadConfig("test/repo", mockReader) |
| 106 | + if err == nil { |
| 107 | + t.Error("expected error for invalid TOML") |
| 108 | + } |
| 109 | + |
| 110 | + if !strings.Contains(err.Error(), "toml") && !strings.Contains(err.Error(), "parse") { |
| 111 | + t.Errorf("expected TOML parse error, got: %v", err) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestReadConfigUsesFilesystemWhenNilReader(t *testing.T) { |
| 116 | + // When nil reader is passed, it should use filesystem reader |
| 117 | + // This test just ensures the fallback logic works |
| 118 | + config, err := ReadConfig("../../test_project", nil) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("unexpected error: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // The test_project directory might not have a codeowners.toml |
| 124 | + // In that case, we should get default config |
| 125 | + if config.Enforcement == nil { |
| 126 | + t.Error("expected enforcement config to be initialized") |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// TestConfigSecurityIsolation verifies that even if filesystem has different config, |
| 131 | +// the git ref reader returns the correct config from the ref |
| 132 | +func TestConfigSecurityIsolation(t *testing.T) { |
| 133 | + // This simulates the security scenario where: |
| 134 | + // - Base ref has max_reviews = 5 |
| 135 | + // - PR branch (filesystem) has max_reviews = 1 (trying to bypass) |
| 136 | + // - We should only see the base ref config (max_reviews = 5) |
| 137 | + |
| 138 | + mockReader := &mockConfigFileReader{ |
| 139 | + files: map[string]string{ |
| 140 | + "test/repo/codeowners.toml": "max_reviews = 5", |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + config, err := ReadConfig("test/repo", mockReader) |
| 145 | + if err != nil { |
| 146 | + t.Fatalf("unexpected error: %v", err) |
| 147 | + } |
| 148 | + |
| 149 | + if config.MaxReviews == nil || *config.MaxReviews != 5 { |
| 150 | + t.Errorf("expected max_reviews = 5 from base ref, got %v", config.MaxReviews) |
| 151 | + } |
| 152 | + |
| 153 | + // Even if filesystem has different value, we're reading from git ref |
| 154 | + // so we should only see the base ref value |
| 155 | +} |
0 commit comments