Add functionality to manage external links for companies
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced the ability to append external links to company profiles through a new API endpoint. - Enhanced the `Company` entity to include a `Links` field for storing external resource links. - Created `AddLinksForm` for validating incoming link data and implemented corresponding logic in the service layer. - Added error handling for link validation, ensuring only valid URLs are accepted and limiting the number of links to 20. - Implemented unit tests for link management functions, including sanitization and merging of links. - Updated relevant API documentation to reflect the new functionality. This update significantly enhances the company management capabilities by allowing the addition of external links, improving the overall user experience and data richness in company profiles.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package company
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMergeCompanyLinks(t *testing.T) {
|
||||
existing := []CompanyLink{
|
||||
{URL: "https://example.com/a"},
|
||||
{URL: "https://example.com/b"},
|
||||
}
|
||||
additional := []CompanyLink{
|
||||
{URL: " https://example.com/b "},
|
||||
{URL: "https://Example.com/C"},
|
||||
{URL: " "},
|
||||
}
|
||||
|
||||
merged, added := mergeCompanyLinks(existing, additional)
|
||||
if len(merged) != 3 {
|
||||
t.Fatalf("mergeCompanyLinks() merged len = %d, want 3", len(merged))
|
||||
}
|
||||
if len(added) != 1 {
|
||||
t.Fatalf("mergeCompanyLinks() added len = %d, want 1", len(added))
|
||||
}
|
||||
if added[0].URL != "https://Example.com/C" {
|
||||
t.Fatalf("added link = %+v, want https://Example.com/C", added[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeCompanyLinks(t *testing.T) {
|
||||
title := " Profile "
|
||||
got, err := sanitizeCompanyLinks([]CompanyLink{
|
||||
{URL: " https://example.com ", Title: &title},
|
||||
{URL: "not-a-url"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("sanitizeCompanyLinks() expected invalid URL error")
|
||||
}
|
||||
|
||||
got, err = sanitizeCompanyLinks([]CompanyLink{
|
||||
{URL: "https://example.com", Title: &title},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("sanitizeCompanyLinks() unexpected error: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("sanitizeCompanyLinks() len = %d, want 1", len(got))
|
||||
}
|
||||
if got[0].URL != "https://example.com" {
|
||||
t.Fatalf("sanitized URL = %q, want https://example.com", got[0].URL)
|
||||
}
|
||||
if got[0].Title == nil || *got[0].Title != "Profile" {
|
||||
t.Fatalf("sanitized title = %+v, want Profile", got[0].Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompanyLinkURLs(t *testing.T) {
|
||||
got := companyLinkURLs([]CompanyLink{
|
||||
{URL: " https://a.test "},
|
||||
{URL: " "},
|
||||
{URL: "https://b.test"},
|
||||
})
|
||||
want := []string{"https://a.test", "https://b.test"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("companyLinkURLs() len = %d, want %d", len(got), len(want))
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("companyLinkURLs()[%d] = %q, want %q", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user