github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+incompatible/api4/job_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/mattermost/mattermost-server/model" 11 "github.com/mattermost/mattermost-server/store" 12 ) 13 14 func TestCreateJob(t *testing.T) { 15 th := Setup().InitBasic().InitSystemAdmin() 16 defer th.TearDown() 17 18 job := &model.Job{ 19 Type: model.JOB_TYPE_DATA_RETENTION, 20 Data: map[string]string{ 21 "thing": "stuff", 22 }, 23 } 24 25 received, resp := th.SystemAdminClient.CreateJob(job) 26 CheckNoError(t, resp) 27 28 defer th.App.Srv.Store.Job().Delete(received.Id) 29 30 job = &model.Job{ 31 Type: model.NewId(), 32 } 33 34 _, resp = th.SystemAdminClient.CreateJob(job) 35 CheckBadRequestStatus(t, resp) 36 37 _, resp = th.Client.CreateJob(job) 38 CheckForbiddenStatus(t, resp) 39 } 40 41 func TestGetJob(t *testing.T) { 42 th := Setup().InitBasic().InitSystemAdmin() 43 defer th.TearDown() 44 45 job := &model.Job{ 46 Id: model.NewId(), 47 Status: model.JOB_STATUS_PENDING, 48 } 49 if result := <-th.App.Srv.Store.Job().Save(job); result.Err != nil { 50 t.Fatal(result.Err) 51 } 52 53 defer th.App.Srv.Store.Job().Delete(job.Id) 54 55 received, resp := th.SystemAdminClient.GetJob(job.Id) 56 CheckNoError(t, resp) 57 58 if received.Id != job.Id || received.Status != job.Status { 59 t.Fatal("incorrect job received") 60 } 61 62 _, resp = th.SystemAdminClient.GetJob("1234") 63 CheckBadRequestStatus(t, resp) 64 65 _, resp = th.Client.GetJob(job.Id) 66 CheckForbiddenStatus(t, resp) 67 68 _, resp = th.SystemAdminClient.GetJob(model.NewId()) 69 CheckNotFoundStatus(t, resp) 70 } 71 72 func TestGetJobs(t *testing.T) { 73 th := Setup().InitBasic().InitSystemAdmin() 74 defer th.TearDown() 75 76 jobType := model.NewId() 77 78 t0 := model.GetMillis() 79 jobs := []*model.Job{ 80 { 81 Id: model.NewId(), 82 Type: jobType, 83 CreateAt: t0 + 1, 84 }, 85 { 86 Id: model.NewId(), 87 Type: jobType, 88 CreateAt: t0, 89 }, 90 { 91 Id: model.NewId(), 92 Type: jobType, 93 CreateAt: t0 + 2, 94 }, 95 } 96 97 for _, job := range jobs { 98 store.Must(th.App.Srv.Store.Job().Save(job)) 99 defer th.App.Srv.Store.Job().Delete(job.Id) 100 } 101 102 received, resp := th.SystemAdminClient.GetJobs(0, 2) 103 CheckNoError(t, resp) 104 105 if len(received) != 2 { 106 t.Fatal("received wrong number of jobs") 107 } else if received[0].Id != jobs[2].Id { 108 t.Fatal("should've received newest job first") 109 } else if received[1].Id != jobs[0].Id { 110 t.Fatal("should've received second newest job second") 111 } 112 113 received, resp = th.SystemAdminClient.GetJobs(1, 2) 114 CheckNoError(t, resp) 115 116 if received[0].Id != jobs[1].Id { 117 t.Fatal("should've received oldest job last") 118 } 119 120 _, resp = th.Client.GetJobs(0, 60) 121 CheckForbiddenStatus(t, resp) 122 } 123 124 func TestGetJobsByType(t *testing.T) { 125 th := Setup().InitBasic().InitSystemAdmin() 126 defer th.TearDown() 127 128 jobType := model.NewId() 129 130 jobs := []*model.Job{ 131 { 132 Id: model.NewId(), 133 Type: jobType, 134 CreateAt: 1000, 135 }, 136 { 137 Id: model.NewId(), 138 Type: jobType, 139 CreateAt: 999, 140 }, 141 { 142 Id: model.NewId(), 143 Type: jobType, 144 CreateAt: 1001, 145 }, 146 { 147 Id: model.NewId(), 148 Type: model.NewId(), 149 CreateAt: 1002, 150 }, 151 } 152 153 for _, job := range jobs { 154 store.Must(th.App.Srv.Store.Job().Save(job)) 155 defer th.App.Srv.Store.Job().Delete(job.Id) 156 } 157 158 received, resp := th.SystemAdminClient.GetJobsByType(jobType, 0, 2) 159 CheckNoError(t, resp) 160 161 if len(received) != 2 { 162 t.Fatal("received wrong number of jobs") 163 } else if received[0].Id != jobs[2].Id { 164 t.Fatal("should've received newest job first") 165 } else if received[1].Id != jobs[0].Id { 166 t.Fatal("should've received second newest job second") 167 } 168 169 received, resp = th.SystemAdminClient.GetJobsByType(jobType, 1, 2) 170 CheckNoError(t, resp) 171 172 if len(received) != 1 { 173 t.Fatal("received wrong number of jobs") 174 } else if received[0].Id != jobs[1].Id { 175 t.Fatal("should've received oldest job last") 176 } 177 178 _, resp = th.SystemAdminClient.GetJobsByType("", 0, 60) 179 CheckNotFoundStatus(t, resp) 180 181 _, resp = th.SystemAdminClient.GetJobsByType(strings.Repeat("a", 33), 0, 60) 182 CheckBadRequestStatus(t, resp) 183 184 _, resp = th.Client.GetJobsByType(jobType, 0, 60) 185 CheckForbiddenStatus(t, resp) 186 } 187 188 func TestCancelJob(t *testing.T) { 189 th := Setup().InitBasic().InitSystemAdmin() 190 defer th.TearDown() 191 192 jobs := []*model.Job{ 193 { 194 Id: model.NewId(), 195 Type: model.NewId(), 196 Status: model.JOB_STATUS_PENDING, 197 }, 198 { 199 Id: model.NewId(), 200 Type: model.NewId(), 201 Status: model.JOB_STATUS_IN_PROGRESS, 202 }, 203 { 204 Id: model.NewId(), 205 Type: model.NewId(), 206 Status: model.JOB_STATUS_SUCCESS, 207 }, 208 } 209 210 for _, job := range jobs { 211 store.Must(th.App.Srv.Store.Job().Save(job)) 212 defer th.App.Srv.Store.Job().Delete(job.Id) 213 } 214 215 _, resp := th.Client.CancelJob(jobs[0].Id) 216 CheckForbiddenStatus(t, resp) 217 218 _, resp = th.SystemAdminClient.CancelJob(jobs[0].Id) 219 CheckNoError(t, resp) 220 221 _, resp = th.SystemAdminClient.CancelJob(jobs[1].Id) 222 CheckNoError(t, resp) 223 224 _, resp = th.SystemAdminClient.CancelJob(jobs[2].Id) 225 CheckInternalErrorStatus(t, resp) 226 227 _, resp = th.SystemAdminClient.CancelJob(model.NewId()) 228 CheckInternalErrorStatus(t, resp) 229 }