youtube-dl-service/backend/main_test.go

145 lines
2.9 KiB
Go
Raw Permalink Normal View History

2021-11-12 08:54:02 +01:00
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
2022-02-10 09:38:38 +01:00
"time"
2021-11-12 08:54:02 +01:00
2021-12-07 13:49:08 +01:00
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
2021-12-07 14:11:10 +01:00
"git.andreafazzi.eu/andrea/youtube-dl-service/logger"
2021-12-02 13:56:39 +01:00
"git.andreafazzi.eu/andrea/youtube-dl-service/task"
2021-11-12 08:54:02 +01:00
"github.com/gin-gonic/gin"
"github.com/remogatto/prettytest"
)
2022-02-10 09:38:38 +01:00
var config *Config
2021-11-12 08:54:02 +01:00
// Start of setup
type testSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
2021-12-10 12:06:04 +01:00
func (t *testSuite) BeforeAll() {
gin.SetMode(gin.ReleaseMode)
2022-02-17 13:56:47 +01:00
logger.SetLevel(logger.DebugLevel)
2022-02-10 09:38:38 +01:00
config = new(Config)
err := ReadConfig("config.yaml", config)
if err != nil {
panic(err)
}
2021-12-10 12:06:04 +01:00
}
2021-11-12 08:54:02 +01:00
// Test the creation of a new task. A new task is created with a POST
// request to the endpoint.
func (t *testSuite) TestCreateTask() {
2022-02-16 12:58:01 +01:00
video, err := postTask("https://www.youtube.com/watch?v=zGDzdps75ns")
2021-12-02 13:56:39 +01:00
if err != nil {
panic(err)
}
2022-02-16 12:58:01 +01:00
t.Equal("zGDzdps75ns", video.ID)
2021-12-02 13:56:39 +01:00
}
// Test the response of a GET /tasks/:id requests.
func (t *testSuite) TestGetTask() {
2022-02-16 12:58:01 +01:00
video, err := postTask("https://www.youtube.com/watch?v=zGDzdps75ns")
2021-12-02 13:56:39 +01:00
if err != nil {
panic(err)
}
2022-02-10 09:38:38 +01:00
timer := time.NewTimer(time.Second * 10)
<-timer.C
2021-12-02 13:56:39 +01:00
// Create a new request
req, err := http.NewRequest("GET", "/task/"+video.ID, nil)
if err != nil {
panic(err)
}
// Create an http recorder
rr := httptest.NewRecorder()
// Set the context and call the handler
c, _ := gin.CreateTestContext(rr)
c.Request = req
getTask(c, video.ID)
// Read the response and assert it.
2021-12-10 13:43:33 +01:00
ts := new(task.Task)
2021-12-02 13:56:39 +01:00
2021-12-10 13:43:33 +01:00
if err := json.Unmarshal(rr.Body.Bytes(), &ts); err != nil {
2021-12-02 13:56:39 +01:00
panic(err)
}
2022-02-16 12:58:01 +01:00
t.Equal("zGDzdps75ns", video.ID)
2021-12-10 13:43:33 +01:00
t.Equal(task.StatusCompleted, ts.Status)
2022-02-17 13:56:47 +01:00
t.Equal(100.0, ts.Percentage)
2021-12-02 13:56:39 +01:00
}
2022-02-10 09:38:38 +01:00
func (t *testSuite) TestPostLogin() {
// Set form values
form := url.Values{}
form.Add("password", "secret")
req, err := http.NewRequest("POST", "/login", strings.NewReader((form.Encode())))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Create an http recorder
rr := httptest.NewRecorder()
// Set the context and call the handler
c, _ := gin.CreateTestContext(rr)
c.Request = req
err = postLogin(c, config)
t.Nil(err)
}
2021-12-07 13:49:08 +01:00
func postTask(ytUrl string) (*downloader.Video, error) {
2021-11-12 08:54:02 +01:00
// Set form values
form := url.Values{}
2021-12-02 13:56:39 +01:00
form.Add("url", ytUrl)
2021-11-12 08:54:02 +01:00
// Create a new request
req, err := http.NewRequest("POST", "/task", strings.NewReader(form.Encode()))
if err != nil {
2021-12-02 13:56:39 +01:00
return nil, err
2021-11-12 08:54:02 +01:00
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Create an http recorder
rr := httptest.NewRecorder()
// Set the context and call the handler
c, _ := gin.CreateTestContext(rr)
c.Request = req
createTask(c)
// Read the response and assert it.
2021-12-07 13:49:08 +01:00
video := new(downloader.Video)
2021-11-12 08:54:02 +01:00
2021-12-02 13:56:39 +01:00
if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil {
return nil, err
2021-11-12 08:54:02 +01:00
}
2021-12-02 13:56:39 +01:00
return video, nil
2021-11-12 08:54:02 +01:00
}