github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api/preference_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/model" 10 ) 11 12 func TestGetAllPreferences(t *testing.T) { 13 th := Setup().InitBasic() 14 defer th.TearDown() 15 16 Client := th.BasicClient 17 user1 := th.BasicUser 18 19 category := model.NewId() 20 21 preferences1 := model.Preferences{ 22 { 23 UserId: user1.Id, 24 Category: category, 25 Name: model.NewId(), 26 }, 27 { 28 UserId: user1.Id, 29 Category: category, 30 Name: model.NewId(), 31 }, 32 { 33 UserId: user1.Id, 34 Category: model.NewId(), 35 Name: model.NewId(), 36 }, 37 } 38 39 Client.Must(Client.SetPreferences(&preferences1)) 40 41 if result, err := Client.GetAllPreferences(); err != nil { 42 t.Fatal(err) 43 } else if data := result.Data.(model.Preferences); len(data) != 4 { 44 t.Fatal("received the wrong number of preferences") 45 } 46 47 th.LoginBasic2() 48 49 if result, err := Client.GetAllPreferences(); err != nil { 50 t.Fatal(err) 51 } else if data := result.Data.(model.Preferences); len(data) == 0 { 52 t.Fatal("received the wrong number of preferences") 53 } 54 } 55 56 func TestSetPreferences(t *testing.T) { 57 th := Setup().InitBasic() 58 defer th.TearDown() 59 60 Client := th.BasicClient 61 user1 := th.BasicUser 62 63 // save 10 preferences 64 var preferences model.Preferences 65 for i := 0; i < 10; i++ { 66 preference := model.Preference{ 67 UserId: user1.Id, 68 Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, 69 Name: model.NewId(), 70 } 71 preferences = append(preferences, preference) 72 } 73 74 if _, err := Client.SetPreferences(&preferences); err != nil { 75 t.Fatal(err) 76 } 77 78 // update 10 preferences 79 for _, preference := range preferences { 80 preference.Value = "1234garbage" 81 } 82 83 if _, err := Client.SetPreferences(&preferences); err != nil { 84 t.Fatal(err) 85 } 86 87 th.LoginBasic2() 88 89 if _, err := Client.SetPreferences(&preferences); err == nil { 90 t.Fatal("shouldn't have been able to update another user's preferences") 91 } 92 } 93 94 func TestGetPreferenceCategory(t *testing.T) { 95 th := Setup().InitBasic() 96 defer th.TearDown() 97 98 Client := th.BasicClient 99 user1 := th.BasicUser 100 101 category := model.NewId() 102 103 preferences1 := model.Preferences{ 104 { 105 UserId: user1.Id, 106 Category: category, 107 Name: model.NewId(), 108 }, 109 { 110 UserId: user1.Id, 111 Category: category, 112 Name: model.NewId(), 113 }, 114 { 115 UserId: user1.Id, 116 Category: model.NewId(), 117 Name: model.NewId(), 118 }, 119 } 120 121 Client.Must(Client.SetPreferences(&preferences1)) 122 123 if result, err := Client.GetPreferenceCategory(category); err != nil { 124 t.Fatal(err) 125 } else if data := result.Data.(model.Preferences); len(data) != 2 { 126 t.Fatal("received the wrong number of preferences") 127 } else if !((data[0] == preferences1[0] && data[1] == preferences1[1]) || (data[0] == preferences1[1] && data[1] == preferences1[0])) { 128 t.Fatal("received incorrect preferences") 129 } 130 131 th.LoginBasic2() 132 133 if result, err := Client.GetPreferenceCategory(category); err != nil { 134 t.Fatal(err) 135 } else if data := result.Data.(model.Preferences); len(data) != 0 { 136 t.Fatal("received the wrong number of preferences") 137 } 138 } 139 140 func TestGetPreference(t *testing.T) { 141 th := Setup().InitBasic() 142 defer th.TearDown() 143 144 Client := th.BasicClient 145 user := th.BasicUser 146 147 preferences := model.Preferences{ 148 { 149 UserId: user.Id, 150 Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, 151 Name: model.NewId(), 152 Value: model.NewId(), 153 }, 154 } 155 156 Client.Must(Client.SetPreferences(&preferences)) 157 158 if result, err := Client.GetPreference(preferences[0].Category, preferences[0].Name); err != nil { 159 t.Fatal(err) 160 } else if data := result.Data.(*model.Preference); *data != preferences[0] { 161 t.Fatal("preference saved incorrectly") 162 } 163 164 preferences[0].Value = model.NewId() 165 Client.Must(Client.SetPreferences(&preferences)) 166 167 if result, err := Client.GetPreference(preferences[0].Category, preferences[0].Name); err != nil { 168 t.Fatal(err) 169 } else if data := result.Data.(*model.Preference); *data != preferences[0] { 170 t.Fatal("preference updated incorrectly") 171 } 172 } 173 174 func TestDeletePreferences(t *testing.T) { 175 th := Setup().InitBasic() 176 defer th.TearDown() 177 178 Client := th.BasicClient 179 user1 := th.BasicUser 180 181 var originalCount int 182 if result, err := Client.GetAllPreferences(); err != nil { 183 t.Fatal(err) 184 } else { 185 originalCount = len(result.Data.(model.Preferences)) 186 } 187 188 // save 10 preferences 189 var preferences model.Preferences 190 for i := 0; i < 10; i++ { 191 preference := model.Preference{ 192 UserId: user1.Id, 193 Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, 194 Name: model.NewId(), 195 } 196 preferences = append(preferences, preference) 197 } 198 199 if _, err := Client.SetPreferences(&preferences); err != nil { 200 t.Fatal(err) 201 } 202 203 // delete 10 preferences 204 th.LoginBasic2() 205 206 if _, err := Client.DeletePreferences(&preferences); err == nil { 207 t.Fatal("shouldn't have been able to delete another user's preferences") 208 } 209 210 th.LoginBasic() 211 if _, err := Client.DeletePreferences(&preferences); err != nil { 212 t.Fatal(err) 213 } 214 215 if result, err := Client.GetAllPreferences(); err != nil { 216 t.Fatal(err) 217 } else if data := result.Data.(model.Preferences); len(data) != originalCount { 218 t.Fatal("should've deleted preferences") 219 } 220 }