|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/flatrun/agent/pkg/models" |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | +) |
| 11 | + |
| 12 | +func TestServiceMetadataCredentialID(t *testing.T) { |
| 13 | + metadata := &models.ServiceMetadata{ |
| 14 | + Name: "test-deployment", |
| 15 | + Type: "custom", |
| 16 | + Networking: models.NetworkingConfig{ |
| 17 | + Expose: true, |
| 18 | + Domain: "test.example.com", |
| 19 | + ContainerPort: 8080, |
| 20 | + }, |
| 21 | + CredentialID: "cred-123", |
| 22 | + } |
| 23 | + |
| 24 | + if metadata.CredentialID != "cred-123" { |
| 25 | + t.Errorf("expected CredentialID 'cred-123', got '%s'", metadata.CredentialID) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestServiceMetadataCredentialIDEmpty(t *testing.T) { |
| 30 | + metadata := &models.ServiceMetadata{ |
| 31 | + Name: "public-deployment", |
| 32 | + Type: "custom", |
| 33 | + } |
| 34 | + |
| 35 | + if metadata.CredentialID != "" { |
| 36 | + t.Errorf("expected empty CredentialID for public deployment, got '%s'", metadata.CredentialID) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestServiceMetadataCredentialIDOmitempty(t *testing.T) { |
| 41 | + metadata := &models.ServiceMetadata{ |
| 42 | + Name: "test", |
| 43 | + Type: "custom", |
| 44 | + } |
| 45 | + |
| 46 | + if metadata.CredentialID != "" { |
| 47 | + t.Error("CredentialID should be empty by default") |
| 48 | + } |
| 49 | + |
| 50 | + metadata.CredentialID = "new-cred-id" |
| 51 | + if metadata.CredentialID != "new-cred-id" { |
| 52 | + t.Errorf("expected CredentialID 'new-cred-id', got '%s'", metadata.CredentialID) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestServiceMetadataCredentialIDYAMLSerialization(t *testing.T) { |
| 57 | + metadata := &models.ServiceMetadata{ |
| 58 | + Name: "private-app", |
| 59 | + Type: "custom", |
| 60 | + CredentialID: "cred-abc-123", |
| 61 | + } |
| 62 | + |
| 63 | + data, err := yaml.Marshal(metadata) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("failed to marshal metadata: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + yamlStr := string(data) |
| 69 | + if !strings.Contains(yamlStr, "credential_id: cred-abc-123") { |
| 70 | + t.Errorf("YAML should contain credential_id field, got:\n%s", yamlStr) |
| 71 | + } |
| 72 | + |
| 73 | + var unmarshaled models.ServiceMetadata |
| 74 | + if err := yaml.Unmarshal(data, &unmarshaled); err != nil { |
| 75 | + t.Fatalf("failed to unmarshal metadata: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + if unmarshaled.CredentialID != "cred-abc-123" { |
| 79 | + t.Errorf("expected CredentialID 'cred-abc-123' after unmarshal, got '%s'", unmarshaled.CredentialID) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestServiceMetadataCredentialIDYAMLOmitsEmpty(t *testing.T) { |
| 84 | + metadata := &models.ServiceMetadata{ |
| 85 | + Name: "public-app", |
| 86 | + Type: "custom", |
| 87 | + } |
| 88 | + |
| 89 | + data, err := yaml.Marshal(metadata) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("failed to marshal metadata: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + yamlStr := string(data) |
| 95 | + if strings.Contains(yamlStr, "credential_id") { |
| 96 | + t.Errorf("YAML should omit credential_id when empty, got:\n%s", yamlStr) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestServiceMetadataCredentialIDJSONSerialization(t *testing.T) { |
| 101 | + metadata := &models.ServiceMetadata{ |
| 102 | + Name: "private-app", |
| 103 | + Type: "custom", |
| 104 | + CredentialID: "cred-xyz-789", |
| 105 | + } |
| 106 | + |
| 107 | + data, err := json.Marshal(metadata) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("failed to marshal metadata: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + jsonStr := string(data) |
| 113 | + if !strings.Contains(jsonStr, `"credential_id":"cred-xyz-789"`) { |
| 114 | + t.Errorf("JSON should contain credential_id field, got:\n%s", jsonStr) |
| 115 | + } |
| 116 | + |
| 117 | + var unmarshaled models.ServiceMetadata |
| 118 | + if err := json.Unmarshal(data, &unmarshaled); err != nil { |
| 119 | + t.Fatalf("failed to unmarshal metadata: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + if unmarshaled.CredentialID != "cred-xyz-789" { |
| 123 | + t.Errorf("expected CredentialID 'cred-xyz-789' after unmarshal, got '%s'", unmarshaled.CredentialID) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestServiceMetadataCredentialIDJSONOmitsEmpty(t *testing.T) { |
| 128 | + metadata := &models.ServiceMetadata{ |
| 129 | + Name: "public-app", |
| 130 | + Type: "custom", |
| 131 | + } |
| 132 | + |
| 133 | + data, err := json.Marshal(metadata) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("failed to marshal metadata: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + jsonStr := string(data) |
| 139 | + if strings.Contains(jsonStr, "credential_id") { |
| 140 | + t.Errorf("JSON should omit credential_id when empty, got:\n%s", jsonStr) |
| 141 | + } |
| 142 | +} |
0 commit comments