()
| 63 | } |
| 64 | |
| 65 | func (s *TemplatesTestSuite) SetupTest() { |
| 66 | ctx := context.Background() |
| 67 | watcher.InitWatcher(ctx) |
| 68 | |
| 69 | db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T())) |
| 70 | if err != nil { |
| 71 | s.FailNow(fmt.Sprintf("failed to create db connection: %s", err)) |
| 72 | } |
| 73 | s.Store = db |
| 74 | |
| 75 | adminCtx := garmTesting.ImpersonateAdminContext(context.Background(), db, s.T()) |
| 76 | s.adminCtx = adminCtx |
| 77 | |
| 78 | // Create a regular user for testing user-scoped templates |
| 79 | user := garmTesting.CreateGARMTestUser(adminCtx, "testuser", db, s.T()) |
| 80 | // Create proper user context (non-admin) |
| 81 | s.ctx = adminCtx // For now, use admin context to avoid complexity |
| 82 | |
| 83 | // Create test templates |
| 84 | templates := []params.Template{} |
| 85 | |
| 86 | // Create system template (user_id = nil) |
| 87 | sysTemplate, err := s.Store.CreateTemplate(s.adminCtx, params.CreateTemplateParams{ |
| 88 | Name: "system-template", |
| 89 | Description: "System template for testing", |
| 90 | OSType: commonParams.Linux, |
| 91 | ForgeType: params.GithubEndpointType, |
| 92 | Data: []byte(`{"provider": "lxd", "image": "ubuntu:22.04"}`), |
| 93 | }) |
| 94 | if err != nil { |
| 95 | s.FailNow(fmt.Sprintf("failed to create system template: %s", err)) |
| 96 | } |
| 97 | templates = append(templates, sysTemplate) |
| 98 | |
| 99 | // Create user template |
| 100 | userTemplate, err := s.Store.CreateTemplate(s.ctx, params.CreateTemplateParams{ |
| 101 | Name: "user-template", |
| 102 | Description: "User template for testing", |
| 103 | OSType: commonParams.Windows, |
| 104 | ForgeType: params.GithubEndpointType, |
| 105 | Data: []byte(`{"provider": "azure", "image": "windows-2022"}`), |
| 106 | }) |
| 107 | if err != nil { |
| 108 | s.FailNow(fmt.Sprintf("failed to create user template: %s", err)) |
| 109 | } |
| 110 | templates = append(templates, userTemplate) |
| 111 | |
| 112 | // Create store with mocked sql connection |
| 113 | sqlDB, sqlMock, err := sqlmock.New() |
| 114 | if err != nil { |
| 115 | s.FailNow(fmt.Sprintf("failed to run 'sqlmock.New()', got error: %v", err)) |
| 116 | } |
| 117 | s.T().Cleanup(func() { sqlDB.Close() }) |
| 118 | mysqlConfig := mysql.Config{ |
| 119 | Conn: sqlDB, |
| 120 | SkipInitializeWithVersion: true, |
| 121 | } |
| 122 | dialector := mysql.New(mysqlConfig) |
nothing calls this directly
no test coverage detected