github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/user_access_token_store.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package storetest 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/store" 11 ) 12 13 func TestUserAccessTokenStore(t *testing.T, ss store.Store) { 14 t.Run("UserAccessTokenSaveGetDelete", func(t *testing.T) { testUserAccessTokenSaveGetDelete(t, ss) }) 15 t.Run("UserAccessTokenDisableEnable", func(t *testing.T) { testUserAccessTokenDisableEnable(t, ss) }) 16 t.Run("UserAccessTokenSearch", func(t *testing.T) { testUserAccessTokenSearch(t, ss) }) 17 } 18 19 func testUserAccessTokenSaveGetDelete(t *testing.T, ss store.Store) { 20 uat := &model.UserAccessToken{ 21 Token: model.NewId(), 22 UserId: model.NewId(), 23 Description: "testtoken", 24 } 25 26 s1 := model.Session{} 27 s1.UserId = uat.UserId 28 s1.Token = uat.Token 29 30 store.Must(ss.Session().Save(&s1)) 31 32 if result := <-ss.UserAccessToken().Save(uat); result.Err != nil { 33 t.Fatal(result.Err) 34 } 35 36 if result := <-ss.UserAccessToken().Get(uat.Id); result.Err != nil { 37 t.Fatal(result.Err) 38 } else if received := result.Data.(*model.UserAccessToken); received.Token != uat.Token { 39 t.Fatal("received incorrect token after save") 40 } 41 42 if result := <-ss.UserAccessToken().GetByToken(uat.Token); result.Err != nil { 43 t.Fatal(result.Err) 44 } else if received := result.Data.(*model.UserAccessToken); received.Token != uat.Token { 45 t.Fatal("received incorrect token after save") 46 } 47 48 if result := <-ss.UserAccessToken().GetByToken("notarealtoken"); result.Err == nil { 49 t.Fatal("should have failed on bad token") 50 } 51 52 if result := <-ss.UserAccessToken().GetByUser(uat.UserId, 0, 100); result.Err != nil { 53 t.Fatal(result.Err) 54 } else if received := result.Data.([]*model.UserAccessToken); len(received) != 1 { 55 t.Fatal("received incorrect number of tokens after save") 56 } 57 58 if result := <-ss.UserAccessToken().GetAll(0, 100); result.Err != nil { 59 t.Fatal(result.Err) 60 } else if received := result.Data.([]*model.UserAccessToken); len(received) != 1 { 61 t.Fatal("received incorrect number of tokens after save") 62 } 63 64 if result := <-ss.UserAccessToken().Delete(uat.Id); result.Err != nil { 65 t.Fatal(result.Err) 66 } 67 68 if err := (<-ss.Session().Get(s1.Token)).Err; err == nil { 69 t.Fatal("should error - session should be deleted") 70 } 71 72 if err := (<-ss.UserAccessToken().GetByToken(s1.Token)).Err; err == nil { 73 t.Fatal("should error - access token should be deleted") 74 } 75 76 s2 := model.Session{} 77 s2.UserId = uat.UserId 78 s2.Token = uat.Token 79 80 store.Must(ss.Session().Save(&s2)) 81 82 if result := <-ss.UserAccessToken().Save(uat); result.Err != nil { 83 t.Fatal(result.Err) 84 } 85 86 if result := <-ss.UserAccessToken().DeleteAllForUser(uat.UserId); result.Err != nil { 87 t.Fatal(result.Err) 88 } 89 90 if err := (<-ss.Session().Get(s2.Token)).Err; err == nil { 91 t.Fatal("should error - session should be deleted") 92 } 93 94 if err := (<-ss.UserAccessToken().GetByToken(s2.Token)).Err; err == nil { 95 t.Fatal("should error - access token should be deleted") 96 } 97 } 98 99 func testUserAccessTokenDisableEnable(t *testing.T, ss store.Store) { 100 uat := &model.UserAccessToken{ 101 Token: model.NewId(), 102 UserId: model.NewId(), 103 Description: "testtoken", 104 } 105 106 s1 := model.Session{} 107 s1.UserId = uat.UserId 108 s1.Token = uat.Token 109 110 store.Must(ss.Session().Save(&s1)) 111 112 if result := <-ss.UserAccessToken().Save(uat); result.Err != nil { 113 t.Fatal(result.Err) 114 } 115 116 if err := (<-ss.UserAccessToken().UpdateTokenDisable(uat.Id)).Err; err != nil { 117 t.Fatal(err) 118 } 119 120 if err := (<-ss.Session().Get(s1.Token)).Err; err == nil { 121 t.Fatal("should error - session should be deleted") 122 } 123 124 s2 := model.Session{} 125 s2.UserId = uat.UserId 126 s2.Token = uat.Token 127 128 store.Must(ss.Session().Save(&s2)) 129 130 if err := (<-ss.UserAccessToken().UpdateTokenEnable(uat.Id)).Err; err != nil { 131 t.Fatal(err) 132 } 133 } 134 135 func testUserAccessTokenSearch(t *testing.T, ss store.Store) { 136 u1 := model.User{} 137 u1.Email = MakeEmail() 138 u1.Username = model.NewId() 139 140 store.Must(ss.User().Save(&u1)) 141 142 uat := &model.UserAccessToken{ 143 Token: model.NewId(), 144 UserId: u1.Id, 145 Description: "testtoken", 146 } 147 148 s1 := model.Session{} 149 s1.UserId = uat.UserId 150 s1.Token = uat.Token 151 152 store.Must(ss.Session().Save(&s1)) 153 154 if result := <-ss.UserAccessToken().Save(uat); result.Err != nil { 155 t.Fatal(result.Err) 156 } 157 158 if result := <-ss.UserAccessToken().Search(uat.Id); result.Err != nil { 159 t.Fatal(result.Err) 160 } else if received := result.Data.([]*model.UserAccessToken); len(received) != 1 { 161 t.Fatal("received incorrect number of tokens after search") 162 } 163 164 if result := <-ss.UserAccessToken().Search(uat.UserId); result.Err != nil { 165 t.Fatal(result.Err) 166 } else if received := result.Data.([]*model.UserAccessToken); len(received) != 1 { 167 t.Fatal("received incorrect number of tokens after search") 168 } 169 170 if result := <-ss.UserAccessToken().Search(u1.Username); result.Err != nil { 171 t.Fatal(result.Err) 172 } else if received := result.Data.([]*model.UserAccessToken); len(received) != 1 { 173 t.Fatal("received incorrect number of tokens after search") 174 } 175 }