Skip to content

Commit 9acd6b4

Browse files
committed
Removed POST and DELETE from /users/follows
1 parent d0ea060 commit 9acd6b4

File tree

2 files changed

+4
-95
lines changed

2 files changed

+4
-95
lines changed

internal/mock_api/endpoints/users/follows.go

+4-60
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,27 @@ import (
77
"log"
88
"net/http"
99

10-
"github.com/mattn/go-sqlite3"
1110
"github.com/twitchdev/twitch-cli/internal/database"
1211
"github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors"
1312
"github.com/twitchdev/twitch-cli/internal/models"
1413
)
1514

1615
var followMethodsSupported = map[string]bool{
1716
http.MethodGet: true,
18-
http.MethodPost: true,
19-
http.MethodDelete: true,
17+
http.MethodPost: false,
18+
http.MethodDelete: false,
2019
http.MethodPatch: false,
2120
http.MethodPut: false,
2221
}
2322

2423
var followScopesByMethod = map[string][]string{
2524
http.MethodGet: {},
26-
http.MethodPost: {"user:edit:follows"},
27-
http.MethodDelete: {"user:edit:follows"},
25+
http.MethodPost: {},
26+
http.MethodDelete: {},
2827
http.MethodPatch: {},
2928
http.MethodPut: {},
3029
}
3130

32-
type PostFollowBody struct {
33-
ToID string `json:"to_id"`
34-
FromID string `json:"from_id"`
35-
}
36-
3731
type FollowsEndpoint struct{}
3832

3933
func (e FollowsEndpoint) Path() string { return "/users/follows" }
@@ -52,10 +46,6 @@ func (e FollowsEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5246
switch r.Method {
5347
case http.MethodGet:
5448
getFollows(w, r)
55-
case http.MethodPost:
56-
postFollows(w, r)
57-
case http.MethodDelete:
58-
deleteFollows(w, r)
5949
default:
6050
w.WriteHeader(http.StatusMethodNotAllowed)
6151
return
@@ -103,49 +93,3 @@ func getFollows(w http.ResponseWriter, r *http.Request) {
10393
json, _ := json.Marshal(body)
10494
w.Write(json)
10595
}
106-
107-
func deleteFollows(w http.ResponseWriter, r *http.Request) {
108-
to := r.URL.Query().Get("to_id")
109-
from := r.URL.Query().Get("from_id")
110-
111-
if len(to) == 0 || len(from) == 0 {
112-
w.WriteHeader(http.StatusBadRequest)
113-
return
114-
}
115-
116-
err := db.NewQuery(r, 100).DeleteFollow(from, to)
117-
if err != nil {
118-
http.Error(w, err.Error(), http.StatusInternalServerError)
119-
return
120-
}
121-
122-
w.WriteHeader(http.StatusNoContent)
123-
}
124-
125-
func postFollows(w http.ResponseWriter, r *http.Request) {
126-
var body PostFollowBody
127-
128-
err := json.NewDecoder(r.Body).Decode(&body)
129-
if err != nil {
130-
mock_errors.WriteBadRequest(w, "error reading body")
131-
return
132-
}
133-
if body.FromID == "" || body.ToID == "" {
134-
mock_errors.WriteBadRequest(w, "from_id and to_id are required")
135-
return
136-
}
137-
138-
err = db.NewQuery(r, 100).AddFollow(database.UserRequestParams{UserID: body.FromID, BroadcasterID: body.ToID})
139-
if err != nil {
140-
if database.DatabaseErrorIs(err, sqlite3.ErrConstraintForeignKey) || database.DatabaseErrorIs(err, sqlite3.ErrConstraintUnique) || database.DatabaseErrorIs(err, sqlite3.ErrConstraintPrimaryKey) {
141-
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
142-
return
143-
}
144-
log.Printf("%#v\n%#v", err, sqlite3.ErrConstraintUnique)
145-
w.WriteHeader(http.StatusInternalServerError)
146-
return
147-
}
148-
149-
w.WriteHeader(http.StatusNoContent)
150-
return
151-
}

internal/mock_api/endpoints/users/users_test.go

-35
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package users
44

55
import (
6-
"bytes"
7-
"encoding/json"
86
"net/http"
97
"testing"
108

@@ -133,37 +131,4 @@ func TestFollows(t *testing.T) {
133131
resp, err = http.DefaultClient.Do(req)
134132
a.Nil(err)
135133
a.Equal(200, resp.StatusCode)
136-
137-
// post
138-
post := PostFollowBody{ToID: "1", FromID: "2"}
139-
b, _ := json.Marshal(post)
140-
req, _ = http.NewRequest(http.MethodPost, ts.URL+FollowsEndpoint{}.Path(), bytes.NewBuffer(b))
141-
q = req.URL.Query()
142-
req.URL.RawQuery = q.Encode()
143-
resp, err = http.DefaultClient.Do(req)
144-
a.Nil(err)
145-
146-
post.FromID = ""
147-
b, _ = json.Marshal(post)
148-
req, _ = http.NewRequest(http.MethodPost, ts.URL+FollowsEndpoint{}.Path(), bytes.NewBuffer(b))
149-
q = req.URL.Query()
150-
req.URL.RawQuery = q.Encode()
151-
resp, err = http.DefaultClient.Do(req)
152-
a.Nil(err)
153-
a.Equal(400, resp.StatusCode)
154-
155-
// delete
156-
req, _ = http.NewRequest(http.MethodDelete, ts.URL+FollowsEndpoint{}.Path(), nil)
157-
q = req.URL.Query()
158-
req.URL.RawQuery = q.Encode()
159-
resp, err = http.DefaultClient.Do(req)
160-
a.Nil(err)
161-
a.Equal(400, resp.StatusCode)
162-
163-
q.Set("to_id", "1")
164-
q.Set("from_id", "2")
165-
req.URL.RawQuery = q.Encode()
166-
resp, err = http.DefaultClient.Do(req)
167-
a.Nil(err)
168-
a.Equal(204, resp.StatusCode)
169134
}

0 commit comments

Comments
 (0)