Skip to content

Commit

Permalink
Removed POST and DELETE from /users/follows
Browse files Browse the repository at this point in the history
  • Loading branch information
Xemdo committed Jan 4, 2023
1 parent d0ea060 commit 9acd6b4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 95 deletions.
64 changes: 4 additions & 60 deletions internal/mock_api/endpoints/users/follows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,27 @@ import (
"log"
"net/http"

"github.com/mattn/go-sqlite3"
"github.com/twitchdev/twitch-cli/internal/database"
"github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors"
"github.com/twitchdev/twitch-cli/internal/models"
)

var followMethodsSupported = map[string]bool{
http.MethodGet: true,
http.MethodPost: true,
http.MethodDelete: true,
http.MethodPost: false,
http.MethodDelete: false,
http.MethodPatch: false,
http.MethodPut: false,
}

var followScopesByMethod = map[string][]string{
http.MethodGet: {},
http.MethodPost: {"user:edit:follows"},
http.MethodDelete: {"user:edit:follows"},
http.MethodPost: {},
http.MethodDelete: {},
http.MethodPatch: {},
http.MethodPut: {},
}

type PostFollowBody struct {
ToID string `json:"to_id"`
FromID string `json:"from_id"`
}

type FollowsEndpoint struct{}

func (e FollowsEndpoint) Path() string { return "/users/follows" }
Expand All @@ -52,10 +46,6 @@ func (e FollowsEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
getFollows(w, r)
case http.MethodPost:
postFollows(w, r)
case http.MethodDelete:
deleteFollows(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
Expand Down Expand Up @@ -103,49 +93,3 @@ func getFollows(w http.ResponseWriter, r *http.Request) {
json, _ := json.Marshal(body)
w.Write(json)
}

func deleteFollows(w http.ResponseWriter, r *http.Request) {
to := r.URL.Query().Get("to_id")
from := r.URL.Query().Get("from_id")

if len(to) == 0 || len(from) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}

err := db.NewQuery(r, 100).DeleteFollow(from, to)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusNoContent)
}

func postFollows(w http.ResponseWriter, r *http.Request) {
var body PostFollowBody

err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
mock_errors.WriteBadRequest(w, "error reading body")
return
}
if body.FromID == "" || body.ToID == "" {
mock_errors.WriteBadRequest(w, "from_id and to_id are required")
return
}

err = db.NewQuery(r, 100).AddFollow(database.UserRequestParams{UserID: body.FromID, BroadcasterID: body.ToID})
if err != nil {
if database.DatabaseErrorIs(err, sqlite3.ErrConstraintForeignKey) || database.DatabaseErrorIs(err, sqlite3.ErrConstraintUnique) || database.DatabaseErrorIs(err, sqlite3.ErrConstraintPrimaryKey) {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
log.Printf("%#v\n%#v", err, sqlite3.ErrConstraintUnique)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusNoContent)
return
}
35 changes: 0 additions & 35 deletions internal/mock_api/endpoints/users/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package users

import (
"bytes"
"encoding/json"
"net/http"
"testing"

Expand Down Expand Up @@ -133,37 +131,4 @@ func TestFollows(t *testing.T) {
resp, err = http.DefaultClient.Do(req)
a.Nil(err)
a.Equal(200, resp.StatusCode)

// post
post := PostFollowBody{ToID: "1", FromID: "2"}
b, _ := json.Marshal(post)
req, _ = http.NewRequest(http.MethodPost, ts.URL+FollowsEndpoint{}.Path(), bytes.NewBuffer(b))
q = req.URL.Query()
req.URL.RawQuery = q.Encode()
resp, err = http.DefaultClient.Do(req)
a.Nil(err)

post.FromID = ""
b, _ = json.Marshal(post)
req, _ = http.NewRequest(http.MethodPost, ts.URL+FollowsEndpoint{}.Path(), bytes.NewBuffer(b))
q = req.URL.Query()
req.URL.RawQuery = q.Encode()
resp, err = http.DefaultClient.Do(req)
a.Nil(err)
a.Equal(400, resp.StatusCode)

// delete
req, _ = http.NewRequest(http.MethodDelete, ts.URL+FollowsEndpoint{}.Path(), nil)
q = req.URL.Query()
req.URL.RawQuery = q.Encode()
resp, err = http.DefaultClient.Do(req)
a.Nil(err)
a.Equal(400, resp.StatusCode)

q.Set("to_id", "1")
q.Set("from_id", "2")
req.URL.RawQuery = q.Encode()
resp, err = http.DefaultClient.Do(req)
a.Nil(err)
a.Equal(204, resp.StatusCode)
}

0 comments on commit 9acd6b4

Please sign in to comment.