github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/migration/helpers_test.go (about)

     1  package migration_test
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  func SetupTeamWithBasicAuth(dbConn *sql.DB, team, basicAuth, auth string) {
    11  	_, err := dbConn.Exec("INSERT INTO teams(name, basic_auth, auth) VALUES($1, $2, $3)", team, basicAuth, auth)
    12  	Expect(err).NotTo(HaveOccurred())
    13  }
    14  
    15  func SetupTeam(dbConn *sql.DB, team, auth string) {
    16  	_, err := dbConn.Exec("INSERT INTO teams(name, auth) VALUES($1, $2)", team, auth)
    17  	Expect(err).NotTo(HaveOccurred())
    18  }
    19  
    20  func SetupPipeline(dbConn *sql.DB, pipeline string) {
    21  	_, err := dbConn.Exec("INSERT INTO pipelines(name, team_id) VALUES($1, 1)", pipeline)
    22  	Expect(err).NotTo(HaveOccurred())
    23  }
    24  
    25  func SetupResource(dbConn *sql.DB, resource, config string) {
    26  	_, err := dbConn.Exec("INSERT INTO resources(name, config, pipeline_id) VALUES($1, $2, 1)", resource, config)
    27  	Expect(err).NotTo(HaveOccurred())
    28  }
    29  
    30  func ExpectResourceWithType(dbConn *sql.DB, resourceName, resourceType string) {
    31  
    32  	Expect(fetchResourceType(dbConn, resourceName)).To(Equal(resourceType))
    33  }
    34  
    35  func ExpectTeamWithUsersAndGroups(dbConn *sql.DB, team string, users, groups []string) {
    36  
    37  	auth := fetchTeamAuth(dbConn, team)
    38  
    39  	Expect(auth["users"]).To(ConsistOf(users))
    40  	Expect(auth["groups"]).To(ConsistOf(groups))
    41  }
    42  
    43  func ExpectTeamWithUsersAndGroupsForRole(dbConn *sql.DB, team string, role string, users, groups []string) {
    44  
    45  	auth := fetchTeamAuth(dbConn, team)
    46  
    47  	authForRole := auth[role].(map[string]interface{})
    48  	Expect(authForRole["users"]).To(ConsistOf(users))
    49  	Expect(authForRole["groups"]).To(ConsistOf(groups))
    50  }
    51  
    52  func ExpectTeamWithGithubProvider(dbConn *sql.DB, team, clientId, clientSecret string) {
    53  
    54  	auth := fetchTeamAuth(dbConn, team)
    55  
    56  	provider, _ := auth["github"].(map[string]interface{})
    57  	Expect(provider["client_id"]).To(Equal(clientId))
    58  	Expect(provider["client_secret"]).To(Equal(clientSecret))
    59  }
    60  
    61  func ExpectTeamWithNoAuthProvider(dbConn *sql.DB, team string, noauth bool) {
    62  
    63  	auth := fetchTeamAuth(dbConn, team)
    64  
    65  	provider, _ := auth["noauth"].(map[string]interface{})
    66  	Expect(provider["noauth"]).To(Equal(noauth))
    67  }
    68  
    69  func ExpectTeamWithBasicAuthProvider(dbConn *sql.DB, team, username, password string) {
    70  
    71  	auth := fetchTeamAuth(dbConn, team)
    72  
    73  	provider, _ := auth["basicauth"].(map[string]interface{})
    74  	Expect(provider["username"]).To(Equal(username))
    75  	Expect(provider["password"]).To(Equal(password))
    76  }
    77  
    78  func ExpectTeamWithoutNoAuthProvider(dbConn *sql.DB, team string) {
    79  
    80  	auth := fetchTeamAuth(dbConn, team)
    81  
    82  	provider, _ := auth["noauth"].(map[string]interface{})
    83  	Expect(provider).To(BeNil())
    84  }
    85  
    86  func ExpectTeamWithoutBasicAuthProvider(dbConn *sql.DB, team string) {
    87  
    88  	auth := fetchTeamAuth(dbConn, team)
    89  
    90  	provider, _ := auth["basicauth"].(map[string]interface{})
    91  	Expect(provider).To(BeNil())
    92  }
    93  
    94  func ExpectTeamWithBasicAuth(dbConn *sql.DB, team, username, password string) {
    95  
    96  	basicAuth := fetchTeamBasicAuth(dbConn, team)
    97  
    98  	Expect(basicAuth["basic_auth_username"]).To(Equal(username))
    99  	Expect(basicAuth["basic_auth_password"]).To(Equal(password))
   100  }
   101  
   102  func ExpectTeamWithAuth(dbConn *sql.DB, team, expectedConfig string) {
   103  	auth := readTeamAuth(dbConn, team)
   104  	Expect(auth).To(MatchJSON(expectedConfig))
   105  }
   106  
   107  func ExpectTeamWithLegacyAuth(dbConn *sql.DB, team, expectedConfig string) {
   108  	auth := readTeamLegacyAuth(dbConn, team)
   109  	Expect(auth).To(MatchJSON(expectedConfig))
   110  }
   111  
   112  func readTeamBasicAuth(dbConn *sql.DB, team string) []byte {
   113  	var auth []byte
   114  	err := dbConn.QueryRow("SELECT basic_auth FROM teams WHERE name = $1", team).Scan(&auth)
   115  	Expect(err).NotTo(HaveOccurred())
   116  	return auth
   117  }
   118  
   119  func fetchTeamBasicAuth(dbConn *sql.DB, team string) map[string]string {
   120  	basicAuth := readTeamBasicAuth(dbConn, team)
   121  	var data map[string]string
   122  	err := json.Unmarshal(basicAuth, &data)
   123  	Expect(err).NotTo(HaveOccurred())
   124  	return data
   125  }
   126  
   127  func readTeamAuth(dbConn *sql.DB, team string) []byte {
   128  	var auth []byte
   129  	err := dbConn.QueryRow("SELECT auth FROM teams WHERE name = $1", team).Scan(&auth)
   130  	Expect(err).NotTo(HaveOccurred())
   131  	return auth
   132  }
   133  
   134  func fetchTeamAuth(dbConn *sql.DB, team string) map[string]interface{} {
   135  	auth := readTeamAuth(dbConn, team)
   136  	var data map[string]interface{}
   137  	err := json.Unmarshal(auth, &data)
   138  	Expect(err).NotTo(HaveOccurred())
   139  	return data
   140  }
   141  
   142  func readTeamLegacyAuth(dbConn *sql.DB, team string) []byte {
   143  	var auth []byte
   144  	err := dbConn.QueryRow("SELECT legacy_auth FROM teams WHERE name = $1", team).Scan(&auth)
   145  	Expect(err).NotTo(HaveOccurred())
   146  	return auth
   147  }
   148  
   149  func fetchResourceType(dbConn *sql.DB, name string) string {
   150  	var raw string
   151  	err := dbConn.QueryRow("SELECT type FROM resources WHERE name = $1", name).Scan(&raw)
   152  	Expect(err).NotTo(HaveOccurred())
   153  	return raw
   154  }