diff options
author | Christopher Speller <crspeller@gmail.com> | 2015-09-16 16:26:56 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2015-09-16 16:26:56 -0400 |
commit | b1f26540ce29cf4ff403cc7b65b20c891ca64118 (patch) | |
tree | fd7a83ebb963dc5f0208dc4dba00aa2f730a364e /store/sql_oauth_store_test.go | |
parent | 4e11fa23d750afbefad099638530b754776a3357 (diff) | |
parent | 47e6a33a4505e13ba4edf37ff1f8fbdadb279ee3 (diff) | |
download | chat-b1f26540ce29cf4ff403cc7b65b20c891ca64118.tar.gz chat-b1f26540ce29cf4ff403cc7b65b20c891ca64118.tar.bz2 chat-b1f26540ce29cf4ff403cc7b65b20c891ca64118.zip |
Merge pull request #693 from mattermost/mm-1419
Implement OAuth2 service provider functionality.
Diffstat (limited to 'store/sql_oauth_store_test.go')
-rw-r--r-- | store/sql_oauth_store_test.go | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/store/sql_oauth_store_test.go b/store/sql_oauth_store_test.go new file mode 100644 index 000000000..08e1388e0 --- /dev/null +++ b/store/sql_oauth_store_test.go @@ -0,0 +1,182 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package store + +import ( + "github.com/mattermost/platform/model" + "testing" +) + +func TestOAuthStoreSaveApp(t *testing.T) { + Setup() + + a1 := model.OAuthApp{} + a1.CreatorId = model.NewId() + a1.Name = "TestApp" + model.NewId() + a1.CallbackUrls = []string{"https://nowhere.com"} + a1.Homepage = "https://nowhere.com" + + if err := (<-store.OAuth().SaveApp(&a1)).Err; err != nil { + t.Fatal(err) + } +} + +func TestOAuthStoreGetApp(t *testing.T) { + Setup() + + a1 := model.OAuthApp{} + a1.CreatorId = model.NewId() + a1.Name = "TestApp" + model.NewId() + a1.CallbackUrls = []string{"https://nowhere.com"} + a1.Homepage = "https://nowhere.com" + Must(store.OAuth().SaveApp(&a1)) + + if err := (<-store.OAuth().GetApp(a1.Id)).Err; err != nil { + t.Fatal(err) + } + + if err := (<-store.OAuth().GetAppByUser(a1.CreatorId)).Err; err != nil { + t.Fatal(err) + } +} + +func TestOAuthStoreUpdateApp(t *testing.T) { + Setup() + + a1 := model.OAuthApp{} + a1.CreatorId = model.NewId() + a1.Name = "TestApp" + model.NewId() + a1.CallbackUrls = []string{"https://nowhere.com"} + a1.Homepage = "https://nowhere.com" + Must(store.OAuth().SaveApp(&a1)) + + a1.CreateAt = 1 + a1.ClientSecret = "pwd" + a1.CreatorId = "12345678901234567890123456" + a1.Name = "NewName" + if result := <-store.OAuth().UpdateApp(&a1); result.Err != nil { + t.Fatal(result.Err) + } else { + ua1 := (result.Data.([2]*model.OAuthApp)[0]) + if ua1.Name != "NewName" { + t.Fatal("name did not update") + } + if ua1.CreateAt == 1 { + t.Fatal("create at should not have updated") + } + if ua1.ClientSecret == "pwd" { + t.Fatal("client secret should not have updated") + } + if ua1.CreatorId == "12345678901234567890123456" { + t.Fatal("creator id should not have updated") + } + } +} + +func TestOAuthStoreSaveAccessData(t *testing.T) { + Setup() + + a1 := model.AccessData{} + a1.AuthCode = model.NewId() + a1.Token = model.NewId() + a1.RefreshToken = model.NewId() + + if err := (<-store.OAuth().SaveAccessData(&a1)).Err; err != nil { + t.Fatal(err) + } +} + +func TestOAuthStoreGetAccessData(t *testing.T) { + Setup() + + a1 := model.AccessData{} + a1.AuthCode = model.NewId() + a1.Token = model.NewId() + a1.RefreshToken = model.NewId() + Must(store.OAuth().SaveAccessData(&a1)) + + if result := <-store.OAuth().GetAccessData(a1.Token); result.Err != nil { + t.Fatal(result.Err) + } else { + ra1 := result.Data.(*model.AccessData) + if a1.Token != ra1.Token { + t.Fatal("tokens didn't match") + } + } + + if err := (<-store.OAuth().GetAccessDataByAuthCode(a1.AuthCode)).Err; err != nil { + t.Fatal(err) + } + + if err := (<-store.OAuth().GetAccessDataByAuthCode("junk")).Err; err != nil { + t.Fatal(err) + } +} + +func TestOAuthStoreRemoveAccessData(t *testing.T) { + Setup() + + a1 := model.AccessData{} + a1.AuthCode = model.NewId() + a1.Token = model.NewId() + a1.RefreshToken = model.NewId() + Must(store.OAuth().SaveAccessData(&a1)) + + if err := (<-store.OAuth().RemoveAccessData(a1.Token)).Err; err != nil { + t.Fatal(err) + } + + if result := <-store.OAuth().GetAccessDataByAuthCode(a1.AuthCode); result.Err != nil { + t.Fatal(result.Err) + } else { + if result.Data != nil { + t.Fatal("did not delete access token") + } + } +} + +func TestOAuthStoreSaveAuthData(t *testing.T) { + Setup() + + a1 := model.AuthData{} + a1.ClientId = model.NewId() + a1.UserId = model.NewId() + a1.Code = model.NewId() + + if err := (<-store.OAuth().SaveAuthData(&a1)).Err; err != nil { + t.Fatal(err) + } +} + +func TestOAuthStoreGetAuthData(t *testing.T) { + Setup() + + a1 := model.AuthData{} + a1.ClientId = model.NewId() + a1.UserId = model.NewId() + a1.Code = model.NewId() + Must(store.OAuth().SaveAuthData(&a1)) + + if err := (<-store.OAuth().GetAuthData(a1.Code)).Err; err != nil { + t.Fatal(err) + } +} + +func TestOAuthStoreRemoveAuthData(t *testing.T) { + Setup() + + a1 := model.AuthData{} + a1.ClientId = model.NewId() + a1.UserId = model.NewId() + a1.Code = model.NewId() + Must(store.OAuth().SaveAuthData(&a1)) + + if err := (<-store.OAuth().RemoveAuthData(a1.Code)).Err; err != nil { + t.Fatal(err) + } + + if err := (<-store.OAuth().GetAuthData(a1.Code)).Err; err == nil { + t.Fatal("should have errored - auth code removed") + } +} |