shopping-list: clear list client

Amolith created

Change summary

internal/cooked/client.go      |  5 +++++
internal/cooked/client_test.go | 33 +++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)

Detailed changes

internal/cooked/client.go 🔗

@@ -122,6 +122,11 @@ func (c *Client) ReadShoppingList(ctx context.Context) (ShoppingList, error) {
 	return response.ShoppingList, nil
 }
 
+// ClearShoppingList logs in when needed and clears the authenticated user's shopping list.
+func (c *Client) ClearShoppingList(ctx context.Context) error {
+	return c.doAuthenticated(ctx, http.MethodDelete, "/api/user/{username}/shopping-list", nil, nil)
+}
+
 // ListRecipes logs in when needed and returns one page of saved recipes.
 func (c *Client) ListRecipes(ctx context.Context, page, limit int) ([]RecipeCard, error) {
 	query := url.Values{}

internal/cooked/client_test.go 🔗

@@ -174,6 +174,21 @@ func TestDeleteRecipeDeletesSavedRecipe(t *testing.T) {
 	}
 }
 
+func TestClearShoppingListACIDShoppingListClear1ClearsAllItems(t *testing.T) {
+	var clearCalls int
+	client, closeServer := newTestClient(t, clearShoppingListTestHandler(t, &clearCalls))
+	defer closeServer()
+
+	err := client.ClearShoppingList(context.Background())
+	if err != nil {
+		t.Fatalf("ClearShoppingList() error = %v", err)
+	}
+
+	if clearCalls != 1 {
+		t.Fatalf("clear calls = %d, want 1", clearCalls)
+	}
+}
+
 func TestUserPathEscapesAuthenticatedUsername(t *testing.T) {
 	client := &Client{authenticatedUsername: "user/name"}
 
@@ -389,6 +404,24 @@ func deleteRecipeTestHandler(t *testing.T, deleteCalls *int) http.HandlerFunc {
 	}
 }
 
+func clearShoppingListTestHandler(t *testing.T, clearCalls *int) http.HandlerFunc {
+	t.Helper()
+
+	return func(w http.ResponseWriter, r *http.Request) {
+		switch r.URL.EscapedPath() {
+		case "/api/public/login":
+			writeLoginResponse(t, w)
+		case "/api/user/returned-user/shopping-list":
+			requireMethod(t, r, http.MethodDelete, "clear shopping list")
+			requireSessionCookie(t, r)
+			(*clearCalls)++
+			w.WriteHeader(http.StatusNoContent)
+		default:
+			t.Fatalf("unexpected path %s", r.URL.EscapedPath())
+		}
+	}
+}
+
 func requirePreviewRecipeTextRequest(t *testing.T, r *http.Request) {
 	t.Helper()