- Added JWT secret rotation feature tests - Implemented config management BDD steps - Created greet service BDD scenarios - Enhanced test server with JWT rotation support - Added comprehensive step definitions Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
package steps
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"dance-lessons-coach/pkg/bdd/testserver"
|
|
"fmt"
|
|
)
|
|
|
|
// GreetSteps holds greet-related step definitions
|
|
type GreetSteps struct {
|
|
client *testserver.Client
|
|
}
|
|
|
|
func NewGreetSteps(client *testserver.Client) *GreetSteps {
|
|
return &GreetSteps{client: client}
|
|
}
|
|
|
|
func (s *GreetSteps) RegisterSteps(ctx interface {
|
|
RegisterStep(string, interface{}) error
|
|
}) error {
|
|
// This will be implemented in the main steps.go file
|
|
return nil
|
|
}
|
|
|
|
// Greet-related steps
|
|
func (s *GreetSteps) iRequestAGreetingFor(name string) error {
|
|
return s.client.Request("GET", fmt.Sprintf("/api/v1/greet/%s", name), nil)
|
|
}
|
|
|
|
func (s *GreetSteps) iRequestTheDefaultGreeting() error {
|
|
return s.client.Request("GET", "/api/v1/greet/", nil)
|
|
}
|
|
|
|
func (s *GreetSteps) iSendPOSTRequestToV2GreetWithName(name string) error {
|
|
// Create JSON request body
|
|
requestBody := map[string]string{"name": name}
|
|
return s.client.Request("POST", "/api/v2/greet", requestBody)
|
|
}
|
|
|
|
func (s *GreetSteps) iSendPOSTRequestToV2GreetWithInvalidJSON(invalidJSON string) error {
|
|
// Send raw invalid JSON
|
|
return s.client.Request("POST", "/api/v2/greet", invalidJSON)
|
|
}
|
|
|
|
func (s *GreetSteps) theServerIsRunningWithV2Enabled() error {
|
|
// Verify the server is running
|
|
if err := s.client.Request("GET", "/api/ready", nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if v2 endpoint is available (should return 405 Method Not Allowed for GET, which means endpoint exists)
|
|
// If v2 is disabled, this will return 404
|
|
resp, err := s.client.CustomRequest("GET", "/api/v2/greet", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// If we get 405, v2 is enabled (endpoint exists but doesn't allow GET)
|
|
if resp.StatusCode == 405 {
|
|
return nil
|
|
}
|
|
|
|
// If we get 404, v2 is disabled - enable it
|
|
if resp.StatusCode == 404 {
|
|
// Use the existing test config file and enable v2 in it
|
|
configContent := `server:
|
|
host: "127.0.0.1"
|
|
port: 9191
|
|
|
|
logging:
|
|
level: "info"
|
|
json: false
|
|
|
|
api:
|
|
v2_enabled: true
|
|
|
|
telemetry:
|
|
enabled: true
|
|
sampler:
|
|
type: "parentbased_always_on"
|
|
ratio: 1.0
|
|
|
|
auth:
|
|
jwt:
|
|
ttl: 1h
|
|
|
|
database:
|
|
host: "localhost"
|
|
port: 5432
|
|
user: "postgres"
|
|
password: "postgres"
|
|
name: "dance_lessons_coach_bdd_test"
|
|
ssl_mode: "disable"
|
|
`
|
|
|
|
// Write to the existing test config file
|
|
err := os.WriteFile("test-config.yaml", []byte(configContent), 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update test config file: %w", err)
|
|
}
|
|
|
|
// Set environment variable to use our config
|
|
os.Setenv("DLC_CONFIG_FILE", "test-config.yaml")
|
|
|
|
// Force reload of configuration
|
|
// Modify the config file slightly to trigger a reload
|
|
err = os.WriteFile("test-config.yaml", []byte(configContent+"\n# trigger v2 reload\n"), 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update test config file: %w", err)
|
|
}
|
|
|
|
// Allow time for config reload
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
// Verify v2 is now enabled
|
|
resp, err = s.client.CustomRequest("GET", "/api/v2/greet", nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to verify v2 enablement: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == 404 {
|
|
return fmt.Errorf("v2 endpoint still not available after enabling")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|