Files
tm_back/internal/customer/company_context_test.go
T
Mazyar 50c018af62
continuous-integration/drone/push Build is passing
Enhance company context handling in customer middleware and routing
- Introduced CompanyContextMiddleware to resolve the active company context for customer requests, ensuring that tender recommendations and company-scoped APIs remain in sync with the database.
- Updated public routes to utilize the new CompanyContextMiddleware alongside the existing AuthMiddleware, improving the handling of company-specific requests.
- Added unit tests for the pickActiveCompanyID function to validate the logic for selecting the appropriate company context based on customer assignments and requested company IDs.

This update enhances the accuracy and reliability of company context management in the application, improving user experience and data consistency.
2026-06-30 22:30:28 +03:30

78 lines
1.7 KiB
Go

package customer
import "testing"
func TestPickActiveCompanyID(t *testing.T) {
assigned := []string{"company-a", "company-b"}
tests := []struct {
name string
assigned []string
token string
requested string
want string
wantErr bool
wantErrType error
}{
{
name: "uses requested company when assigned",
assigned: assigned,
token: "company-a",
requested: "company-b",
want: "company-b",
},
{
name: "rejects requested company that is not assigned",
assigned: assigned,
token: "company-a",
requested: "company-c",
wantErr: true,
wantErrType: errCompanyNotAssigned,
},
{
name: "keeps valid token company",
assigned: assigned,
token: "company-b",
want: "company-b",
},
{
name: "falls back to first assignment when token company was removed",
assigned: []string{"company-b"},
token: "company-a",
want: "company-b",
},
{
name: "returns empty when customer has no companies",
assigned: nil,
token: "company-a",
want: "",
},
{
name: "uses first assignment when token is empty",
assigned: assigned,
want: "company-a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := pickActiveCompanyID(tt.assigned, tt.token, tt.requested)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
if tt.wantErrType != nil && err != tt.wantErrType {
t.Fatalf("expected error %v, got %v", tt.wantErrType, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Fatalf("pickActiveCompanyID() = %q, want %q", got, tt.want)
}
})
}
}