| 1 | using System.Net; |
| 2 | using System.Net.Http.Json; |
| 3 | using System.Text.Json; |
| 4 | |
| 5 | namespace AgentForge.Tests; |
| 6 | |
| 7 | public sealed class ForgeOrgVisibilityTests |
| 8 | { |
| 9 | private readonly ForgeWebApplicationFactory _factory = new() { PluginBundle = "ams-showcase" }; |
| 10 | private readonly HttpClient _client; |
| 11 | private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; |
| 12 | |
| 13 | public ForgeOrgVisibilityTests() |
| 14 | { |
| 15 | const string bootstrap = "test-bootstrap-org-vis"; |
| 16 | Environment.SetEnvironmentVariable("FORGE_BOOTSTRAP_TOKEN", bootstrap); |
| 17 | _client = _factory.CreateClient(); |
| 18 | _client.DefaultRequestHeaders.Authorization = new("Bearer", bootstrap); |
| 19 | } |
| 20 | |
| 21 | [Fact] |
| 22 | public async Task Private_repo_is_hidden_from_anonymous_list_and_get() |
| 23 | { |
| 24 | _client.DefaultRequestHeaders.Authorization = new("Bearer", "test-bootstrap-org-vis"); |
| 25 | |
| 26 | var createOrg = await _client.PostAsJsonAsync( |
| 27 | "/api/v1/orgs", |
| 28 | new { slug = "pilot-org", displayName = "Pilot" }, |
| 29 | new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); |
| 30 | createOrg.EnsureSuccessStatusCode(); |
| 31 | |
| 32 | var createRepo = await _client.PostAsJsonAsync( |
| 33 | "/api/v1/repos", |
| 34 | new |
| 35 | { |
| 36 | name = "secret-pilot", |
| 37 | org = "pilot-org", |
| 38 | visibility = "private", |
| 39 | }, |
| 40 | new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); |
| 41 | createRepo.EnsureSuccessStatusCode(); |
| 42 | |
| 43 | using var anon = _factory.CreateClient(); |
| 44 | var list = await anon.GetFromJsonAsync<JsonElement>("/api/v1/repos", JsonOptions); |
| 45 | Assert.Equal(0, list.GetProperty("total").GetInt32()); |
| 46 | |
| 47 | var get = await anon.GetAsync("/api/v1/repos/pilot-org/secret-pilot"); |
| 48 | Assert.Equal(HttpStatusCode.NotFound, get.StatusCode); |
| 49 | } |
| 50 | |
| 51 | [Fact] |
| 52 | public async Task Public_repo_is_visible_without_auth() |
| 53 | { |
| 54 | using var anon = _factory.CreateClient(); |
| 55 | var createRepo = await anon.PostAsJsonAsync( |
| 56 | "/api/v1/repos", |
| 57 | new { name = "public-showcase", visibility = "public" }, |
| 58 | new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); |
| 59 | createRepo.EnsureSuccessStatusCode(); |
| 60 | |
| 61 | var list = await anon.GetFromJsonAsync<JsonElement>("/api/v1/repos", JsonOptions); |
| 62 | Assert.Equal(1, list.GetProperty("total").GetInt32()); |
| 63 | |
| 64 | var get = await anon.GetFromJsonAsync<JsonElement>("/api/v1/repos/public-showcase", JsonOptions); |
| 65 | Assert.Equal("public", get.GetProperty("visibility").GetString()); |
| 66 | } |
| 67 | } |
| 68 | |