-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes_movie_test.go
102 lines (86 loc) · 2.18 KB
/
routes_movie_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
type testStore struct {
movieId int64
movies []*Movie
}
func (t testStore) Open() error {
return nil
}
func (t testStore) Close() error {
return nil
}
func (t testStore) GetMovies() ([]*Movie, error) {
return t.movies, nil
}
func (t testStore) GetMovieById(id int64) (*Movie, error) {
for _, m := range t.movies {
if m.ID == id {
return m, nil
}
}
return nil, nil
}
func (t testStore) CreateMovie(m *Movie) error {
t.movieId++
m.ID = t.movieId
t.movies = append(t.movies, m)
return nil
}
func (t testStore) FindUser(username string, password string) (bool, error) {
return true, nil
}
func TestMovieCreateUnit(t *testing.T) {
srv := newServer()
srv.store = &testStore{}
p := struct {
Title string `json:"title"`
ReleaseDate string `json:"release_date"`
Duration int `json:"duration"`
TrailerURL string `json:"trailer_url"`
}{
Title: "Inception",
ReleaseDate: "2010-07-18",
Duration: 148,
TrailerURL: "https://google.com",
}
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(p)
assert.Nil(t, err)
r := httptest.NewRequest("POST", "/api/movies/", &buf)
w := httptest.NewRecorder()
srv.handleMovieCreate()(w, r)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestMovieCreateIntegration(t *testing.T) {
srv := newServer()
srv.store = &testStore{}
p := struct {
Title string `json:"title"`
ReleaseDate string `json:"release_date"`
Duration int `json:"duration"`
TrailerURL string `json:"trailer_url"`
}{
Title: "Inception",
ReleaseDate: "2010-07-18",
Duration: 148,
TrailerURL: "https://google.com",
}
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(p)
assert.Nil(t, err)
r := httptest.NewRequest("POST", "/api/movies/", &buf)
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NzAzNDAxODksImlhdCI6MTY3MDMzNjU4OSwidXNlcm5hbWUiOiJnb2xhbmcifQ.lHG4VWKAONP6AD1t6SqkwTk7KTMjzFNTa6aTGf9Bvs0"
r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
w := httptest.NewRecorder()
srv.serveHTTP(w, r)
assert.Equal(t, http.StatusOK, w.Code)
}