-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
95 lines (74 loc) · 2.19 KB
/
helpers.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
package test
import (
"encoding/json"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/margostino/climateline-processor/api"
"github.com/margostino/climateline-processor/bot"
"github.com/margostino/climateline-processor/domain"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func setJobSecret(request *http.Request) {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("CLIMATELINE_JOB_SECRET")))
request.Header.Set("Content-Type", "application/json")
}
func setBotSecret(request *http.Request, secret string) {
request.Header.Set("X-Telegram-Bot-Api-Secret-Token", secret)
request.Header.Set("Content-Type", "application/json")
}
func postCache(items []domain.Item) (*httptest.ResponseRecorder, error) {
request, err := mockCachePostRequest(items)
if err != nil {
return nil, err
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.Cache)
handler.ServeHTTP(rr, &request)
return rr, nil
}
func postDelete() (*httptest.ResponseRecorder, error) {
request, err := mockCacheDeleteRequest()
if err != nil {
return nil, err
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.Cache)
handler.ServeHTTP(rr, &request)
return rr, nil
}
func getCache(id string) (*httptest.ResponseRecorder, error) {
request, err := mockCacheGetRequest(id)
if err != nil {
return nil, err
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.Cache)
handler.ServeHTTP(rr, &request)
return rr, nil
}
func putCache(id string, newTitle string) (*httptest.ResponseRecorder, error) {
request, err := mockCachePutRequest(id, newTitle)
if err != nil {
return nil, err
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.Cache)
handler.ServeHTTP(rr, &request)
return rr, nil
}
func parseBotResponse(response *httptest.ResponseRecorder) (*domain.BotResponse, error) {
var botResponse domain.BotResponse
err := json.NewDecoder(response.Body).Decode(&botResponse)
return &botResponse, err
}
func createBotMessage(input string) *tgbotapi.Message {
return &tgbotapi.Message{
Text: input,
}
}
func validateInput(t *testing.T, input string) {
assertValidInput(bot.IsValidInput(createBotMessage(input)), input, t)
}