Add tests documenting Notes API parameter format bug
This commit is contained in:
parent
e4862b8e9b
commit
9145157ba7
2 changed files with 155 additions and 0 deletions
154
notes_test.go
154
notes_test.go
|
|
@ -170,3 +170,157 @@ func TestNotes_Delete(t *testing.T) {
|
|||
t.Error("expected DELETE to be called")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotes_Create_RealAPIFormat tests that the client sends the correct
|
||||
// nested parameter format expected by the real Checkvist API.
|
||||
// The API expects: {"comment": {"comment": "text"}}
|
||||
// Not the flat format: {"comment": "text"}
|
||||
//
|
||||
// This test documents the current FAILING behavior - it should pass once
|
||||
// the parameter format is fixed.
|
||||
func TestNotes_Create_RealAPIFormat(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/auth/login.json":
|
||||
json.NewEncoder(w).Encode(map[string]string{"token": "test-token"})
|
||||
case "/checklists/1/tasks/101/comments.json":
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("expected POST, got %s", r.Method)
|
||||
}
|
||||
|
||||
// Parse the request body as raw JSON to check structure
|
||||
var rawBody map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&rawBody); err != nil {
|
||||
t.Fatalf("failed to decode request: %v", err)
|
||||
}
|
||||
|
||||
// The real API expects nested format: {"comment": {"comment": "text"}}
|
||||
commentField, ok := rawBody["comment"]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
return
|
||||
}
|
||||
|
||||
// Check if it's the nested format (map) or flat format (string)
|
||||
switch v := commentField.(type) {
|
||||
case map[string]interface{}:
|
||||
// Correct nested format: {"comment": {"comment": "text"}}
|
||||
innerComment, ok := v["comment"].(string)
|
||||
if !ok || innerComment == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
return
|
||||
}
|
||||
// Success - return created note
|
||||
response := Note{
|
||||
ID: 600,
|
||||
TaskID: 101,
|
||||
Comment: innerComment,
|
||||
CreatedAt: NewAPITime(time.Now()),
|
||||
UpdatedAt: NewAPITime(time.Now()),
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
case string:
|
||||
// Wrong flat format: {"comment": "text"} - this is what the current code sends
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
}
|
||||
default:
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient("user@example.com", "api-key", WithBaseURL(server.URL))
|
||||
note, err := client.Notes(1, 101).Create(context.Background(), "Test note content")
|
||||
|
||||
// Currently this FAILS because the code sends flat format {"comment": "text"}
|
||||
// but the API expects nested format {"comment": {"comment": "text"}}
|
||||
if err != nil {
|
||||
if apiErr, ok := err.(*APIError); ok && apiErr.StatusCode == 400 {
|
||||
t.Skipf("KNOWN BUG: Notes.Create sends wrong parameter format: %v", err)
|
||||
}
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if note.ID != 600 {
|
||||
t.Errorf("expected ID 600, got %d", note.ID)
|
||||
}
|
||||
if note.Comment != "Test note content" {
|
||||
t.Errorf("expected comment 'Test note content', got %s", note.Comment)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotes_Update_RealAPIFormat tests that Update also uses the correct nested format.
|
||||
func TestNotes_Update_RealAPIFormat(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/auth/login.json":
|
||||
json.NewEncoder(w).Encode(map[string]string{"token": "test-token"})
|
||||
case "/checklists/1/tasks/101/comments/501.json":
|
||||
if r.Method != http.MethodPut {
|
||||
t.Errorf("expected PUT, got %s", r.Method)
|
||||
}
|
||||
|
||||
var rawBody map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&rawBody); err != nil {
|
||||
t.Fatalf("failed to decode request: %v", err)
|
||||
}
|
||||
|
||||
commentField, ok := rawBody["comment"]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
return
|
||||
}
|
||||
|
||||
switch v := commentField.(type) {
|
||||
case map[string]interface{}:
|
||||
innerComment, ok := v["comment"].(string)
|
||||
if !ok || innerComment == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
return
|
||||
}
|
||||
response := Note{
|
||||
ID: 501,
|
||||
TaskID: 101,
|
||||
Comment: innerComment,
|
||||
UpdatedAt: NewAPITime(time.Now()),
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
case string:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"message":"comment[comment] parameter is required"}`))
|
||||
}
|
||||
default:
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient("user@example.com", "api-key", WithBaseURL(server.URL))
|
||||
note, err := client.Notes(1, 101).Update(context.Background(), 501, "Updated content")
|
||||
|
||||
if err != nil {
|
||||
if apiErr, ok := err.(*APIError); ok && apiErr.StatusCode == 400 {
|
||||
t.Skipf("KNOWN BUG: Notes.Update sends wrong parameter format: %v", err)
|
||||
}
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if note.Comment != "Updated content" {
|
||||
t.Errorf("expected comment 'Updated content', got %s", note.Comment)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue