summaryrefslogtreecommitdiffstats
path: root/web/web_test.go
blob: 8d40810b5e0aba09fe768abe88bf4099fd56f47d (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package web

import (
	"net/http"
	"net/url"
	"strings"
	"testing"
	"time"

	"github.com/mattermost/platform/api"
	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/store"
	"github.com/mattermost/platform/utils"
)

var ApiClient *model.Client
var URL string

func Setup() {
	if api.Srv == nil {
		utils.LoadConfig("config.json")
		api.NewServer()
		api.StartServer()
		api.InitApi()
		InitWeb()
		URL = "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress
		ApiClient = model.NewClient(URL)

		api.Srv.Store.MarkSystemRanUnitTests()
	}
}

func TearDown() {
	if api.Srv != nil {
		api.StopServer()
	}
}

func TestStatic(t *testing.T) {
	Setup()

	// add a short delay to make sure the server is ready to receive requests
	time.Sleep(1 * time.Second)

	resp, err := http.Get(URL + "/static/images/favicon.ico")

	if err != nil {
		t.Fatalf("got error while trying to get static files %v", err)
	} else if resp.StatusCode != http.StatusOK {
		t.Fatalf("couldn't get static files %v", resp.StatusCode)
	}
}

func TestGetAccessToken(t *testing.T) {
	Setup()

	team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	rteam, _ := ApiClient.CreateTeam(&team)

	user := model.User{TeamId: rteam.Data.(*model.Team).Id, Email: strings.ToLower(model.NewId()) + "corey+test@test.com", Password: "pwd"}
	ruser := ApiClient.Must(ApiClient.CreateUser(&user, "")).Data.(*model.User)
	store.Must(api.Srv.Store.User().VerifyEmail(ruser.Id))

	app := &model.OAuthApp{Name: "TestApp" + model.NewId(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}

	if !utils.Cfg.ServiceSettings.EnableOAuthServiceProvider {
		data := url.Values{"grant_type": []string{"junk"}, "client_id": []string{"12345678901234567890123456"}, "client_secret": []string{"12345678901234567890123456"}, "code": []string{"junk"}, "redirect_uri": []string{app.CallbackUrls[0]}}

		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - oauth providing turned off")
		}
	} else {

		ApiClient.Must(ApiClient.LoginById(ruser.Id, "pwd"))
		app = ApiClient.Must(ApiClient.RegisterApp(app)).Data.(*model.OAuthApp)

		redirect := ApiClient.Must(ApiClient.AllowOAuth(model.AUTHCODE_RESPONSE_TYPE, app.Id, app.CallbackUrls[0], "all", "123")).Data.(map[string]string)["redirect"]
		rurl, _ := url.Parse(redirect)

		ApiClient.Logout()

		data := url.Values{"grant_type": []string{"junk"}, "client_id": []string{app.Id}, "client_secret": []string{app.ClientSecret}, "code": []string{rurl.Query().Get("code")}, "redirect_uri": []string{app.CallbackUrls[0]}}

		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - bad grant type")
		}

		data.Set("grant_type", model.ACCESS_TOKEN_GRANT_TYPE)
		data.Set("client_id", "")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - missing client id")
		}
		data.Set("client_id", "junk")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - bad client id")
		}

		data.Set("client_id", app.Id)
		data.Set("client_secret", "")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - missing client secret")
		}

		data.Set("client_secret", "junk")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - bad client secret")
		}

		data.Set("client_secret", app.ClientSecret)
		data.Set("code", "")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - missing code")
		}

		data.Set("code", "junk")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - bad code")
		}

		data.Set("code", rurl.Query().Get("code"))
		data.Set("redirect_uri", "junk")
		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - non-matching redirect uri")
		}

		// reset data for successful request
		data.Set("grant_type", model.ACCESS_TOKEN_GRANT_TYPE)
		data.Set("client_id", app.Id)
		data.Set("client_secret", app.ClientSecret)
		data.Set("code", rurl.Query().Get("code"))
		data.Set("redirect_uri", app.CallbackUrls[0])

		token := ""
		if result, err := ApiClient.GetAccessToken(data); err != nil {
			t.Fatal(err)
		} else {
			rsp := result.Data.(*model.AccessResponse)
			if len(rsp.AccessToken) == 0 {
				t.Fatal("access token not returned")
			} else {
				token = rsp.AccessToken
			}
			if rsp.TokenType != model.ACCESS_TOKEN_TYPE {
				t.Fatal("access token type incorrect")
			}
		}

		if result, err := ApiClient.DoApiGet("/users/profiles?access_token="+token, "", ""); err != nil {
			t.Fatal(err)
		} else {
			userMap := model.UserMapFromJson(result.Body)
			if len(userMap) == 0 {
				t.Fatal("user map empty - did not get results correctly")
			}
		}

		if _, err := ApiClient.DoApiGet("/users/profiles", "", ""); err == nil {
			t.Fatal("should have failed - no access token provided")
		}

		if _, err := ApiClient.DoApiGet("/users/profiles?access_token=junk", "", ""); err == nil {
			t.Fatal("should have failed - bad access token provided")
		}

		ApiClient.SetOAuthToken(token)
		if result, err := ApiClient.DoApiGet("/users/profiles", "", ""); err != nil {
			t.Fatal(err)
		} else {
			userMap := model.UserMapFromJson(result.Body)
			if len(userMap) == 0 {
				t.Fatal("user map empty - did not get results correctly")
			}
		}

		if _, err := ApiClient.GetAccessToken(data); err == nil {
			t.Fatal("should have failed - tried to reuse auth code")
		}

		ApiClient.ClearOAuthToken()
	}
}

func TestIncomingWebhook(t *testing.T) {
	Setup()

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = ApiClient.Must(ApiClient.CreateTeam(team)).Data.(*model.Team)

	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
	user = ApiClient.Must(ApiClient.CreateUser(user, "")).Data.(*model.User)
	store.Must(api.Srv.Store.User().VerifyEmail(user.Id))

	ApiClient.LoginByEmail(team.Name, user.Email, "pwd")

	channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel1 = ApiClient.Must(ApiClient.CreateChannel(channel1)).Data.(*model.Channel)

	if utils.Cfg.ServiceSettings.EnableIncomingWebhooks {
		hook1 := &model.IncomingWebhook{ChannelId: channel1.Id}
		hook1 = ApiClient.Must(ApiClient.CreateIncomingWebhook(hook1)).Data.(*model.IncomingWebhook)

		payload := "payload={\"text\": \"test text\"}"
		if _, err := ApiClient.PostToWebhook(hook1.Id, payload); err != nil {
			t.Fatal(err)
		}

		payload = "payload={\"text\": \"\"}"
		if _, err := ApiClient.PostToWebhook(hook1.Id, payload); err == nil {
			t.Fatal("should have errored - no text to post")
		}

		payload = "payload={\"text\": \"test text\", \"channel\": \"junk\"}"
		if _, err := ApiClient.PostToWebhook(hook1.Id, payload); err == nil {
			t.Fatal("should have errored - bad channel")
		}

		payload = "payload={\"text\": \"test text\"}"
		if _, err := ApiClient.PostToWebhook("abc123", payload); err == nil {
			t.Fatal("should have errored - bad hook")
		}
	} else {
		if _, err := ApiClient.PostToWebhook("123", "123"); err == nil {
			t.Fatal("should have failed - webhooks turned off")
		}
	}
}

func TestZZWebTearDown(t *testing.T) {
	// *IMPORTANT*
	// This should be the last function in any test file
	// that calls Setup()
	// Should be in the last file too sorted by name
	time.Sleep(2 * time.Second)
	TearDown()
}