| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-06-16 03:23:57 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestUserDataAccess(t *testing.T) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Convey("Testing DB", t, func() { | 
					
						
							| 
									
										
										
										
											2018-12-19 02:44:17 +08:00
										 |  |  | 		ss := InitTestDB(t) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 		Convey("Creating a user", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 			cmd := &models.CreateUserCommand{ | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 				Email: "usertest@test.com", | 
					
						
							|  |  |  | 				Name:  "user name", | 
					
						
							|  |  |  | 				Login: "user_test_login", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err := CreateUser(context.Background(), cmd) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Loading a user", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.GetUserByIdQuery{Id: cmd.Result.Id} | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 				err := GetUserById(&query) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(query.Result.Email, ShouldEqual, "usertest@test.com") | 
					
						
							|  |  |  | 				So(query.Result.Password, ShouldEqual, "") | 
					
						
							|  |  |  | 				So(query.Result.Rands, ShouldHaveLength, 10) | 
					
						
							|  |  |  | 				So(query.Result.Salt, ShouldHaveLength, 10) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 		Convey("Given 5 users", func() { | 
					
						
							|  |  |  | 			var err error | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 			var cmd *models.CreateUserCommand | 
					
						
							|  |  |  | 			users := []models.User{} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			for i := 0; i < 5; i++ { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				cmd = &models.CreateUserCommand{ | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 					Email: fmt.Sprint("user", i, "@test.com"), | 
					
						
							|  |  |  | 					Name:  fmt.Sprint("user", i), | 
					
						
							|  |  |  | 					Login: fmt.Sprint("loginuser", i), | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-06-16 03:23:57 +08:00
										 |  |  | 				err = CreateUser(context.Background(), cmd) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				users = append(users, cmd.Result) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Can return the first page of users and a total count", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.SearchUsersQuery{Query: "", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 3) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 5) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			Convey("Can return the second page of users and a total count", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.SearchUsersQuery{Query: "", Page: 2, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 2) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 5) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			Convey("Can return list of users matching query on user name", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.SearchUsersQuery{Query: "use", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 3) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 5) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query = models.SearchUsersQuery{Query: "ser1", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 1) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 1) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query = models.SearchUsersQuery{Query: "USER1", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 1) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 1) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query = models.SearchUsersQuery{Query: "idontexist", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 0) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 0) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			Convey("Can return list of users matching query on email", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.SearchUsersQuery{Query: "ser1@test.com", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 1) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 1) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			Convey("Can return list of users matching query on login name", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.SearchUsersQuery{Query: "loginuser1", Page: 1, Limit: 3} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				err = SearchUsers(&query) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result.Users), ShouldEqual, 1) | 
					
						
							|  |  |  | 				So(query.Result.TotalCount, ShouldEqual, 1) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 			Convey("can return list users based on their auth type", func() { | 
					
						
							|  |  |  | 				// add users to auth table
 | 
					
						
							|  |  |  | 				for index, user := range users { | 
					
						
							|  |  |  | 					authModule := "killa" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// define every second user as ldap
 | 
					
						
							|  |  |  | 					if index%2 == 0 { | 
					
						
							|  |  |  | 						authModule = "ldap" | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					cmd2 := &models.SetAuthInfoCommand{ | 
					
						
							|  |  |  | 						UserId:     user.Id, | 
					
						
							|  |  |  | 						AuthModule: authModule, | 
					
						
							|  |  |  | 						AuthId:     "gorilla", | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					err = SetAuthInfo(cmd2) | 
					
						
							|  |  |  | 					So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				query := models.SearchUsersQuery{AuthModule: "ldap"} | 
					
						
							|  |  |  | 				err = SearchUsers(&query) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(query.Result.Users, ShouldHaveLength, 3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				zero, second, fourth := false, false, false | 
					
						
							|  |  |  | 				for _, user := range query.Result.Users { | 
					
						
							|  |  |  | 					if user.Name == "user0" { | 
					
						
							|  |  |  | 						zero = true | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if user.Name == "user2" { | 
					
						
							|  |  |  | 						second = true | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if user.Name == "user4" { | 
					
						
							|  |  |  | 						fourth = true | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(zero, ShouldBeTrue) | 
					
						
							|  |  |  | 				So(second, ShouldBeTrue) | 
					
						
							|  |  |  | 				So(fourth, ShouldBeTrue) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			Convey("when a user is an org member and has been assigned permissions", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				err = AddOrgUser(&models.AddOrgUserCommand{LoginOrEmail: users[1].Login, Role: models.ROLE_VIEWER, OrgId: users[0].OrgId, UserId: users[1].Id}) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				testHelperUpdateDashboardAcl(1, models.DashboardAcl{DashboardId: 1, OrgId: users[0].OrgId, UserId: users[1].Id, Permission: models.PERMISSION_EDIT}) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				err = SavePreferences(&models.SavePreferencesCommand{UserId: users[1].Id, OrgId: users[0].OrgId, HomeDashboardId: 1, Theme: "dark"}) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 				Convey("when the user is deleted", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 					err = DeleteUser(&models.DeleteUserCommand{UserId: users[1].Id}) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 					So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Convey("Should delete connected org users and permissions", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 						query := &models.GetOrgUsersQuery{OrgId: users[0].OrgId} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 						err = GetOrgUsersForTest(query) | 
					
						
							|  |  |  | 						So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						So(len(query.Result), ShouldEqual, 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 						permQuery := &models.GetDashboardAclInfoListQuery{DashboardId: 1, OrgId: users[0].OrgId} | 
					
						
							| 
									
										
										
										
											2017-06-20 05:30:54 +08:00
										 |  |  | 						err = GetDashboardAclInfoList(permQuery) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 						So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						So(len(permQuery.Result), ShouldEqual, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 						prefsQuery := &models.GetPreferencesQuery{OrgId: users[0].OrgId, UserId: users[1].Id} | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 						err = GetPreferences(prefsQuery) | 
					
						
							|  |  |  | 						So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						So(prefsQuery.Result.OrgId, ShouldEqual, 0) | 
					
						
							|  |  |  | 						So(prefsQuery.Result.UserId, ShouldEqual, 0) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2018-12-19 02:44:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Convey("when retreiving signed in user for orgId=0 result should return active org id", func() { | 
					
						
							|  |  |  | 					ss.CacheService.Flush() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 					query := &models.GetSignedInUserQuery{OrgId: users[1].OrgId, UserId: users[1].Id} | 
					
						
							| 
									
										
										
										
											2018-12-19 02:44:17 +08:00
										 |  |  | 					err := ss.GetSignedInUserWithCache(query) | 
					
						
							|  |  |  | 					So(err, ShouldBeNil) | 
					
						
							|  |  |  | 					So(query.Result, ShouldNotBeNil) | 
					
						
							|  |  |  | 					So(query.OrgId, ShouldEqual, users[1].OrgId) | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 					err = SetUsingOrg(&models.SetUsingOrgCommand{UserId: users[1].Id, OrgId: users[0].OrgId}) | 
					
						
							| 
									
										
										
										
											2018-12-19 02:44:17 +08:00
										 |  |  | 					So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 					query = &models.GetSignedInUserQuery{OrgId: 0, UserId: users[1].Id} | 
					
						
							| 
									
										
										
										
											2018-12-19 02:44:17 +08:00
										 |  |  | 					err = ss.GetSignedInUserWithCache(query) | 
					
						
							|  |  |  | 					So(err, ShouldBeNil) | 
					
						
							|  |  |  | 					So(query.Result, ShouldNotBeNil) | 
					
						
							|  |  |  | 					So(query.Result.OrgId, ShouldEqual, users[0].OrgId) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					cacheKey := newSignedInUserCacheKey(query.Result.OrgId, query.UserId) | 
					
						
							|  |  |  | 					_, found := ss.CacheService.Get(cacheKey) | 
					
						
							|  |  |  | 					So(found, ShouldBeTrue) | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-05-31 18:22:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("When batch disabling users", func() { | 
					
						
							|  |  |  | 				userIdsToDisable := []int64{} | 
					
						
							|  |  |  | 				for i := 0; i < 3; i++ { | 
					
						
							|  |  |  | 					userIdsToDisable = append(userIdsToDisable, users[i].Id) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				disableCmd := models.BatchDisableUsersCommand{UserIds: userIdsToDisable, IsDisabled: true} | 
					
						
							| 
									
										
										
										
											2019-05-31 18:22:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				err = BatchDisableUsers(&disableCmd) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("Should disable all provided users", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 					query := models.SearchUsersQuery{} | 
					
						
							| 
									
										
										
										
											2019-05-31 18:22:22 +08:00
										 |  |  | 					err = SearchUsers(&query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					So(query.Result.TotalCount, ShouldEqual, 5) | 
					
						
							|  |  |  | 					for _, user := range query.Result.Users { | 
					
						
							|  |  |  | 						shouldBeDisabled := false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						// Check if user id is in the userIdsToDisable list
 | 
					
						
							|  |  |  | 						for _, disabledUserId := range userIdsToDisable { | 
					
						
							|  |  |  | 							if user.Id == disabledUserId { | 
					
						
							|  |  |  | 								So(user.IsDisabled, ShouldBeTrue) | 
					
						
							|  |  |  | 								shouldBeDisabled = true | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						// Otherwise user shouldn't be disabled
 | 
					
						
							|  |  |  | 						if !shouldBeDisabled { | 
					
						
							|  |  |  | 							So(user.IsDisabled, ShouldBeFalse) | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-03-07 23:03:54 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-12-02 01:28:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Given one grafana admin user", func() { | 
					
						
							|  |  |  | 			var err error | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 			createUserCmd := &models.CreateUserCommand{ | 
					
						
							| 
									
										
										
										
											2018-12-02 01:28:10 +08:00
										 |  |  | 				Email:   fmt.Sprint("admin", "@test.com"), | 
					
						
							|  |  |  | 				Name:    fmt.Sprint("admin"), | 
					
						
							|  |  |  | 				Login:   fmt.Sprint("admin"), | 
					
						
							|  |  |  | 				IsAdmin: true, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			err = CreateUser(context.Background(), createUserCmd) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Cannot make themselves a non-admin", func() { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				updateUserPermsCmd := models.UpdateUserPermissionsCommand{IsGrafanaAdmin: false, UserId: 1} | 
					
						
							| 
									
										
										
										
											2018-12-02 01:28:10 +08:00
										 |  |  | 				updatePermsError := UpdateUserPermissions(&updateUserPermsCmd) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				So(updatePermsError, ShouldEqual, models.ErrLastGrafanaAdmin) | 
					
						
							| 
									
										
										
										
											2018-12-02 01:28:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | 				query := models.GetUserByIdQuery{Id: createUserCmd.Result.Id} | 
					
						
							| 
									
										
										
										
											2018-12-02 01:28:10 +08:00
										 |  |  | 				getUserError := GetUserById(&query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(getUserError, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(query.Result.IsAdmin, ShouldEqual, true) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 16:50:38 +08:00
										 |  |  | func GetOrgUsersForTest(query *models.GetOrgUsersQuery) error { | 
					
						
							|  |  |  | 	query.Result = make([]*models.OrgUserDTO, 0) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 	sess := x.Table("org_user") | 
					
						
							| 
									
										
										
										
											2019-03-04 23:57:29 +08:00
										 |  |  | 	sess.Join("LEFT ", x.Dialect().Quote("user"), fmt.Sprintf("org_user.user_id=%s.id", x.Dialect().Quote("user"))) | 
					
						
							| 
									
										
										
										
											2017-06-15 05:45:30 +08:00
										 |  |  | 	sess.Where("org_user.org_id=?", query.OrgId) | 
					
						
							|  |  |  | 	sess.Cols("org_user.org_id", "org_user.user_id", "user.email", "user.login", "org_user.role") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := sess.Find(&query.Result) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } |