Skip to content

Commit

Permalink
feat: rework DELETE /api/v1/topics support
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Jan 2, 2024
1 parent 8cc83a2 commit cabe44c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
31 changes: 27 additions & 4 deletions api/topics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package api

import (
"net/http"
"net/url"

"github.com/gorilla/schema"
)

type GetTopicsParams struct {
Value *string `schema:"value,omitempty"`
Q *string `schema:"q,omitempty"`
Expand All @@ -14,8 +21,6 @@ func (client *Client) GetTopics(params *GetTopicsParams, opts ...Option) ([]*Top
return topics, nil
}

// TODO support DELETE /topics

type PostTopicsParams struct {
Challenge int `json:"challenge"`
Type string `json:"type"`
Expand All @@ -38,6 +43,24 @@ func (client *Client) GetTopic(id string, opts ...Option) (*Topic, error) {
return topic, nil
}

func (client *Client) DeleteTopic(id string, opts ...Option) error {
return delete(client, "/topics/"+id, nil, nil, opts...)
type DeleteTopicArgs struct {
ID string `schema:"target_id"`
Type string `schema:"type"`
}

// TODO fix this endpoint API instability, should reconsider using a DELETE method with a JSON body rather that URL-encoded parameters as for all other endpoints
func (client *Client) DeleteTopic(params *DeleteTopicArgs, opts ...Option) error {
// Build request
req, _ := http.NewRequest(http.MethodDelete, "/topics", nil)
req = applyOpts(req, opts...)

// Encode parameters
val := url.Values{}
if err := schema.NewEncoder().Encode(params, val); err != nil {
return err
}
req.URL.RawQuery = val.Encode()

// Throw it to CTFd
return call(client, req, nil)
}
10 changes: 9 additions & 1 deletion deploy/integration/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"strconv"
"testing"

"github.com/ctfer-io/go-ctfd/api"
Expand Down Expand Up @@ -161,7 +162,7 @@ func Test_F_Setup(t *testing.T) {
if !assert.Nil(err, "got error: %s", err) {
return
}
_, err = client.PostTopics(&api.PostTopicsParams{
topic, err := client.PostTopics(&api.PostTopicsParams{
Challenge: chall.ID,
Type: "challenge", // required as the resource can't be determined by CTFd
Value: "Inspection",
Expand Down Expand Up @@ -197,6 +198,13 @@ func Test_F_Setup(t *testing.T) {

// 7. Delete the challenge
// XXX the strconv should not occur
err = client.DeleteTopic(&api.DeleteTopicArgs{
ID: strconv.Itoa(topic.ID),
Type: "challenge",
})
if !assert.Nil(err, "got error: %s", err) {
return
}
err = client.DeleteChallenge(chall.ID)
if !assert.Nil(err, "got error: %s", err) {
return
Expand Down

0 comments on commit cabe44c

Please sign in to comment.