Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add publish video test #135

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/web/models/Publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import "GuGoTik/src/rpc/feed"

type ListPublishReq struct {
Token string `form:"token" binding:"required"`
ActorId uint32 `form:"actor_id" binding:"required"`
UserId uint32 `form:"user_id" binding:"required"`
}
Expand All @@ -12,3 +13,12 @@ type ListPublishRes struct {
StatusMsg string `json:"status_msg"`
VideoList []*feed.Video `json:"vide_list"`
}

type ActionPublishReq struct {
ActorId uint32 `form:"actor_id" binding:"required"`
}

type ActionPublishRes struct {
StatusCode int `json:"status_code"`
StatusMsg string `json:"status_msg"`
}
8 changes: 4 additions & 4 deletions test/web/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestActionComment_Add(t *testing.T) {
method := "POST"
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "c8e50d04-ebc6-4c36-ad67-9b46a61a2197")
q.Add("token", token)
q.Add("actor_id", "1")
q.Add("video_id", "0")
q.Add("action_type", "1")
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestActionComment_Delete(t *testing.T) {
method := "POST"
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "c8e50d04-ebc6-4c36-ad67-9b46a61a2197")
q.Add("token", token)
q.Add("actor_id", "1")
q.Add("video_id", "0")
q.Add("action_type", "2")
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestListComment(t *testing.T) {
method := "GET"
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "c8e50d04-ebc6-4c36-ad67-9b46a61a2197")
q.Add("token", token)
q.Add("actor_id", "1")
q.Add("video_id", "0")
req.URL.RawQuery = q.Encode()
Expand All @@ -102,7 +102,7 @@ func TestCountComment(t *testing.T) {
method := "GET"
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "c8e50d04-ebc6-4c36-ad67-9b46a61a2197")
q.Add("token", token)
q.Add("actor_id", "1")
q.Add("video_id", "0")
req.URL.RawQuery = q.Encode()
Expand Down
12 changes: 9 additions & 3 deletions test/web/favorite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ var favoriteBaseUrl = "http://127.0.0.1:37000/douyin/favorite"
func TestActionFavorite_Do(t *testing.T) {
url := favoriteBaseUrl + "/action"
method := "POST"
req, _ := http.NewRequest(method, url, nil)
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "") // replace token, video_id
q.Add("video_id", "1948195853")
q.Add("action_type", "1")
req.URL.RawQuery = q.Encode()

assert.Empty(t, err)

res, err := client.Do(req)
assert.Empty(t, err)
defer func(Body io.ReadCloser) {
Expand All @@ -39,13 +41,15 @@ func TestActionFavorite_Do(t *testing.T) {
func TestActionFavorite_Cancel(t *testing.T) {
url := favoriteBaseUrl + "/action"
method := "POST"
req, _ := http.NewRequest(method, url, nil)
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "") // replace token, video_id
q.Add("video_id", "1948195853")
q.Add("action_type", "2")
req.URL.RawQuery = q.Encode()

assert.Empty(t, err)

res, err := client.Do(req)
assert.Empty(t, err)
defer func(Body io.ReadCloser) {
Expand All @@ -64,12 +68,14 @@ func TestActionFavorite_Cancel(t *testing.T) {
func TestListFavorite(t *testing.T) {
url := favoriteBaseUrl + "/list"
method := "POST"
req, _ := http.NewRequest(method, url, nil)
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", "") // replace token, user_id
q.Add("user_id", "1")
req.URL.RawQuery = q.Encode()

assert.Empty(t, err)

res, err := client.Do(req)
assert.Empty(t, err)
defer func(Body io.ReadCloser) {
Expand Down
53 changes: 52 additions & 1 deletion test/web/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package web

import (
"GuGoTik/src/web/models"
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"mime/multipart"
"net/http"
"os"
"testing"
)

Expand All @@ -14,8 +17,9 @@ func TestListVideo(t *testing.T) {
method := "GET"
req, err := http.NewRequest(method, url, nil)
q := req.URL.Query()
q.Add("token", token)
q.Add("actor_id", "1")
q.Add("video_id", "0")
q.Add("user_id", "2")
req.URL.RawQuery = q.Encode()

assert.Empty(t, err)
Expand All @@ -34,3 +38,50 @@ func TestListVideo(t *testing.T) {
assert.Empty(t, err)
assert.Equal(t, 0, ListPublishRes.StatusCode)
}

func TestPublishVideo(t *testing.T) {
url := "http://127.0.0.1:37000/douyin/publish/action"
method := "POST"
filePath := "/home/yangfeng/Repos/youthcamp/videos/upload_video_1_1080p.mp4"

payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)

file, err := os.Open(filePath)
assert.Empty(t, err)
defer func(file *os.File) {
err := file.Close()
assert.Empty(t, err)
}(file)

fileWriter, err := writer.CreateFormFile("data", file.Name())
assert.Empty(t, err)

_, err = io.Copy(fileWriter, file)
assert.Empty(t, err)

_ = writer.WriteField("token", token)
_ = writer.WriteField("title", "午夜小电影")

err = writer.Close()
assert.Empty(t, err)

req, err := http.NewRequest(method, url, payload)
assert.Empty(t, err)

req.Header.Set("Content-Type", writer.FormDataContentType())

res, err := client.Do(req)
assert.Empty(t, err)
defer func(Body io.ReadCloser) {
err := Body.Close()
assert.Empty(t, err)
}(res.Body)

body, err := io.ReadAll(res.Body)
assert.Empty(t, err)
actionPublishRes := &models.ActionPublishRes{}
err = json.Unmarshal(body, &actionPublishRes)
assert.Empty(t, err)
assert.Equal(t, 0, actionPublishRes.StatusCode)
}
3 changes: 3 additions & 0 deletions test/web/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package web

const token = "a594ab0f-4d95-480d-8977-f62eb692776a"
Loading