File: telegram_generator_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,332 kB
  • sloc: sh: 61; makefile: 5
file content (131 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package telegram_test

import (
	"fmt"
	"io"
	"strings"

	"github.com/jarcoal/httpmock"
	"github.com/mattn/go-colorable"
	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"

	"github.com/nicholas-fedor/shoutrrr/pkg/services/telegram"
)

const (
	mockToken   = `0:MockToken`
	mockAPIBase = "https://api.telegram.org/bot" + mockToken + "/"
)

var (
	userOut    *gbytes.Buffer
	userIn     *gbytes.Buffer
	userInMono io.Writer
)

func mockTyped(a ...any) {
	fmt.Fprint(userOut, a...)
	fmt.Fprint(userOut, "\n")
}

func dumpBuffers() {
	for _, line := range strings.Split(string(userIn.Contents()), "\n") {
		fmt.Fprint(ginkgo.GinkgoWriter, "> ", line, "\n")
	}

	for _, line := range strings.Split(string(userOut.Contents()), "\n") {
		fmt.Fprint(ginkgo.GinkgoWriter, "< ", line, "\n")
	}
}

func mockAPI(endpoint string) string {
	return mockAPIBase + endpoint
}

var _ = ginkgo.Describe("TelegramGenerator", func() {
	ginkgo.BeforeEach(func() {
		userOut = gbytes.NewBuffer()
		userIn = gbytes.NewBuffer()
		userInMono = colorable.NewNonColorable(userIn)
		httpmock.Activate()
	})
	ginkgo.AfterEach(func() {
		httpmock.DeactivateAndReset()
	})
	ginkgo.It("should return the ", func() {
		gen := telegram.Generator{
			Reader: userOut,
			Writer: userInMono,
		}

		resultChannel := make(chan string, 1)

		httpmock.RegisterResponder(
			"GET",
			mockAPI(`getMe`),
			httpmock.NewJsonResponderOrPanic(200, &struct {
				OK     bool
				Result *telegram.User
			}{
				true, &telegram.User{
					ID:       1,
					IsBot:    true,
					Username: "mockbot",
				},
			}),
		)

		httpmock.RegisterResponder(
			"POST",
			mockAPI(`getUpdates`),
			httpmock.NewJsonResponderOrPanic(200, &struct {
				OK     bool
				Result []telegram.Update
			}{
				true,
				[]telegram.Update{
					{
						ChatMemberUpdate: &telegram.ChatMemberUpdate{
							Chat:          &telegram.Chat{Type: `channel`, Title: `mockChannel`},
							OldChatMember: &telegram.ChatMember{Status: `kicked`},
							NewChatMember: &telegram.ChatMember{Status: `administrator`},
						},
					},
					{
						Message: &telegram.Message{
							Text: "hi!",
							From: &telegram.User{Username: `mockUser`},
							Chat: &telegram.Chat{Type: `private`, ID: 667, Username: `mockUser`},
						},
					},
				},
			}),
		)

		go func() {
			defer ginkgo.GinkgoRecover()
			conf, err := gen.Generate(nil, nil, nil)

			gomega.Expect(conf).ToNot(gomega.BeNil())
			gomega.Expect(err).NotTo(gomega.HaveOccurred())
			resultChannel <- conf.GetURL().String()
		}()

		defer dumpBuffers()

		mockTyped(mockToken)
		mockTyped(`no`)

		gomega.Eventually(userIn).
			Should(gbytes.Say(`Got a bot chat member update for mockChannel, status was changed from kicked to administrator`))
		gomega.Eventually(userIn).
			Should(gbytes.Say(`Got 1 chat ID\(s\) so far\. Want to add some more\?`))
		gomega.Eventually(userIn).Should(gbytes.Say(`Selected chats:`))
		gomega.Eventually(userIn).Should(gbytes.Say(`667 \(private\) @mockUser`))

		gomega.Eventually(resultChannel).
			Should(gomega.Receive(gomega.Equal(`telegram://0:MockToken@telegram?chats=667&preview=No`)))
	})
})