-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
159 lines (123 loc) · 5.8 KB
/
service_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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2015 The Golang.hr Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package main ...
package main
import (
"fmt"
"testing"
"github.com/Sirupsen/logrus"
"github.com/golanghr/platform/options"
"github.com/golanghr/platform/utils"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
pb "github.com/golanghr/slack-invite/protos"
. "github.com/smartystreets/goconvey/convey"
)
var (
alreadyInTeamEmail = "[email protected]"
)
func getSlackOptions() (options.Options, error) {
return options.New("memo", map[string]interface{}{
"service-name": utils.GetFromEnvOr("SLACK_SERVICE_NAME", "Slack Invite"),
"service-description": utils.GetFromEnvOr("SLACK_SERVICE_DESCRIPTION", "Golang.hr Slack Invite is a small automation service written on top of Golang.hr Platform."),
"service-version": getFloat(utils.GetFromEnvOr("SLACK_SERVICE_VERSION", "0.1")),
"formatter": "text",
"level": logrus.DebugLevel,
"manager-interrupt-wait-timeout": getInt(utils.GetFromEnvOr("SLACK_SERVICE_MANAGER_INTERRUPT_TIMEOUT", "10")),
"grpc-listen-forever": getBool(utils.GetFromEnvOr("SLACK_SERVICE_GRPC_LISTEN_FOREVER", "true")),
"grpc-addr": utils.GetFromEnvOr("SLACK_SERVICE_GRPC_ADDR", ":4772"),
"grpc-tls": getBool(utils.GetFromEnvOr("SLACK_SERVICE_GRPC_TLS", "true")),
"grpc-tls-cert": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_CERT", "test_data/server.crt"),
"grpc-tls-key": utils.GetFromEnvOr("HELLO_SERVICE_GRPC_TLS_KEY", "test_data/server.key"),
"grpc-tls-domain": utils.GetFromEnvOr("SLACK_SERVICE_GRPC_TLS_DOMAIN", "golang.hr"),
"http-addr": utils.GetFromEnvOr("SLACK_SERVICE_HTTP_ADDR", ":8500"),
"http-listen-forever": getBool(utils.GetFromEnvOr("SLACK_SERVICE_HTTP_LISTEN_FOREVER", "true")),
"slack-team-name": utils.GetFromEnvOr("SLACK_TEAM_NAME", "golanghr"),
"slack-token": utils.GetFromEnvOr("SLACK_TOKEN", ""),
"slack-api-debug": getBool(utils.GetFromEnvOr("SLACK_API_DEBUG", "false")),
})
}
func getSlackService(opts options.Options) (*Service, error) {
return NewService(opts, logger)
}
func TestSlackInviteService(t *testing.T) {
opts, _ := getSlackOptions()
service, err := getSlackService(opts)
Convey("Should be service without any errors", t, func() {
So(service, ShouldHaveSameTypeAs, &Service{})
So(err, ShouldBeNil)
})
}
func TestStatsPb(t *testing.T) {
opts, _ := getSlackOptions()
// We gotta update these as there are multiple tests spawning out multiple listeners
opts.Set("http-addr", ":8501")
opts.Set("grpc-addr", ":4773")
service, err := getSlackService(opts)
Convey("Should be service without any errors", t, func() {
So(service, ShouldHaveSameTypeAs, &Service{})
So(err, ShouldBeNil)
})
Convey("Test Fucking protobuff", t, func() {
pb, err := service.Slack.GetStatsPb()
So(err, ShouldBeNil)
fmt.Printf("Pb response: %q \n", pb)
})
}
func TestInviteEndpoint(t *testing.T) {
opts, _ := getSlackOptions()
// We gotta update these as there are multiple tests spawning out multiple listeners
opts.Set("http-addr", ":8502")
opts.Set("grpc-addr", ":4774")
service, err := getSlackService(opts)
Convey("Should be service without any errors", t, func() {
So(service, ShouldHaveSameTypeAs, &Service{})
So(err, ShouldBeNil)
})
go func() { service.Start() }()
defer service.Terminate()
address, _ := service.Options.Get("grpc-addr")
var gopts []grpc.DialOption
var creds credentials.TransportAuthenticator
domain, _ := opts.Get("grpc-tls-domain")
creds, _ = credentials.NewClientTLSFromFile("test_data/server.crt", domain.String())
gopts = append(gopts, grpc.WithTransportCredentials(creds))
Convey("By passing invalid first name we get error that one must be set", t, func() {
conn, err := grpc.Dial(address.String(), gopts...)
So(err, ShouldBeNil)
defer conn.Close()
client := pb.NewSlackClient(conn)
_, err = client.Invite(context.Background(), &pb.Request{})
So(err.Error(), ShouldContainSubstring, "First name must be provided in order")
})
Convey("By passing invalid last name we get error that one must be provided", t, func() {
conn, err := grpc.Dial(address.String(), gopts...)
So(err, ShouldBeNil)
defer conn.Close()
client := pb.NewSlackClient(conn)
_, err = client.Invite(context.Background(), &pb.Request{FirstName: "Test"})
So(err.Error(), ShouldContainSubstring, "Last name must be provided in order")
})
Convey("By passing invalid email we get error that one must be set", t, func() {
conn, err := grpc.Dial(address.String(), gopts...)
So(err, ShouldBeNil)
defer conn.Close()
client := pb.NewSlackClient(conn)
_, err = client.Invite(context.Background(), &pb.Request{FirstName: "Test", LastName: "Testing"})
So(err.Error(), ShouldContainSubstring, "Valid email must be provided")
_, err = client.Invite(context.Background(), &pb.Request{FirstName: "Test", LastName: "Testing", Email: "iamnotvalid"})
So(err.Error(), ShouldContainSubstring, "Valid email must be provided")
})
Convey("By passing already invited email we get already invited error", t, func() {
conn, err := grpc.Dial(address.String(), gopts...)
So(err, ShouldBeNil)
defer conn.Close()
client := pb.NewSlackClient(conn)
_, err = client.Invite(context.Background(), &pb.Request{FirstName: "Test", LastName: "Testing"})
So(err.Error(), ShouldContainSubstring, "Valid email must be provided")
_, err = client.Invite(context.Background(), &pb.Request{FirstName: "Test", LastName: "Testing", Email: alreadyInTeamEmail})
So(err.Error(), ShouldContainSubstring, "Failed to invite to team: already_in_team")
})
}