package user_test import ( "bytes" "encoding/json" "net/http" "testing" "github.com/stretchr/testify/assert" "github.com/yaoapp/yao/openapi" "github.com/yaoapp/yao/openapi/tests/testutils" ) func TestUserLogin(t *testing.T) { // Initialize test environment serverURL := testutils.Prepare(t) defer testutils.Clean() // Get base URL from server config baseURL := "" if openapi.Server != nil && openapi.Server.Config != nil { baseURL = openapi.Server.Config.BaseURL } // Register a test client first (needed for user.Load validation) testClient := testutils.RegisterTestClient(t, "User Login Test Client", []string{"https://localhost/callback"}) defer testutils.CleanupTestClient(t, testClient.ClientID) // Note: user.Load is automatically called by openapi.Load in testutils.Prepare // Test entry endpoint (unified login/register, currently empty implementation) testCases := []struct { name string method string endpoint string body map[string]interface{} expectCode int }{ { "post entry without credentials", "POST", "/user/entry", map[string]interface{}{}, 200, // Currently empty implementation, may change when implemented }, { "post entry with credentials", "POST", "/user/entry", map[string]interface{}{ "username": "testuser", "password": "testpass", }, 200, // Currently empty implementation, may change when implemented }, { "post entry with email", "POST", "/user/entry", map[string]interface{}{ "email": "test@example.com", "password": "testpass", }, 200, // Currently empty implementation, may change when implemented }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { requestURL := serverURL + baseURL + tc.endpoint // Prepare request body var req *http.Request var err error if tc.method == "POST" { bodyBytes, _ := json.Marshal(tc.body) req, err = http.NewRequest(tc.method, requestURL, bytes.NewBuffer(bodyBytes)) req.Header.Set("Content-Type", "application/json") } else { req, err = http.NewRequest(tc.method, requestURL, nil) } assert.NoError(t, err, "Should create HTTP request") client := &http.Client{} resp, err := client.Do(req) assert.NoError(t, err, "HTTP request should succeed") if resp != nil { defer resp.Body.Close() assert.Equal(t, tc.expectCode, resp.StatusCode, "Expected status code %d", tc.expectCode) t.Logf("Login test %s: status=%d", tc.name, resp.StatusCode) // Note: Since login is currently not implemented (empty function), // we can't test actual login functionality yet. // This test serves as a placeholder for when login is implemented. } }) } } func TestUserLoginValidation(t *testing.T) { // Initialize test environment serverURL := testutils.Prepare(t) defer testutils.Clean() // Get base URL from server config baseURL := "" if openapi.Server != nil && openapi.Server.Config != nil { baseURL = openapi.Server.Config.BaseURL } // Note: user.Load is automatically called by openapi.Load in testutils.Prepare // Test various login validation scenarios // Note: These tests are prepared for when login validation is implemented testCases := []struct { name string body map[string]interface{} expected string // Expected behavior description }{ { "empty credentials", map[string]interface{}{}, "Should handle empty credentials gracefully", }, { "missing password", map[string]interface{}{ "username": "testuser", }, "Should handle missing password", }, { "missing username", map[string]interface{}{ "password": "testpass", }, "Should handle missing username", }, { "invalid json format", nil, // Will send invalid JSON "Should handle invalid JSON format", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { requestURL := serverURL + baseURL + "/user/entry" var req *http.Request var err error if tc.body == nil { // Send invalid JSON req, err = http.NewRequest("POST", requestURL, bytes.NewBufferString("invalid json")) } else { bodyBytes, _ := json.Marshal(tc.body) req, err = http.NewRequest("POST", requestURL, bytes.NewBuffer(bodyBytes)) } req.Header.Set("Content-Type", "application/json") assert.NoError(t, err, "Should create HTTP request") client := &http.Client{} resp, err := client.Do(req) assert.NoError(t, err, "HTTP request should succeed") if resp != nil { defer resp.Body.Close() t.Logf("Validation test %s: status=%d, expected=%s", tc.name, resp.StatusCode, tc.expected) // Note: Since login validation is not implemented yet, // we can't assert specific status codes. // These tests will be updated when login is implemented. } }) } }