From efdfbfd090cff4497a24807ef4d172f33cab554d Mon Sep 17 00:00:00 2001 From: Kayra Date: Mon, 1 Apr 2024 13:37:17 +0300 Subject: [PATCH] comments --- internal/certdb/certdb.go | 20 ++++++++-------- internal/certdb/certdb_test.go | 44 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/internal/certdb/certdb.go b/internal/certdb/certdb.go index 17e68cb..f163e6a 100644 --- a/internal/certdb/certdb.go +++ b/internal/certdb/certdb.go @@ -7,7 +7,7 @@ import ( _ "github.com/mattn/go-sqlite3" ) -const queryCreateTable = "CREATE TABLE IF NOT EXISTS %s (CSR VARCHAR PRIMARY KEY UNIQUE NOT NULL, Certificate VARCHAR)" +const queryCreateTable = "CREATE TABLE IF NOT EXISTS %s (CSR VARCHAR PRIMARY KEY UNIQUE NOT NULL, Certificate VARCHAR DEFAULT '')" const queryGetAllCSRs = "SELECT rowid, * FROM %s" const queryGetCSR = "SELECT rowid, * FROM %s WHERE CSR=?" @@ -15,7 +15,7 @@ const queryCreateCSR = "INSERT INTO %s (CSR) VALUES (?)" const queryUpdateCSR = "UPDATE %s SET Certificate=? WHERE CSR=?" const queryDeleteCSR = "DELETE FROM %s WHERE CSR=?" -type CertificateRequests struct { +type CertificateRequestsRepository struct { table string conn *sql.DB } @@ -23,10 +23,10 @@ type CertificateRequests struct { type CertificateRequest struct { ID int CSR string - Certificate *string + Certificate string } -func (db *CertificateRequests) RetrieveAll() ([]CertificateRequest, error) { +func (db *CertificateRequestsRepository) RetrieveAll() ([]CertificateRequest, error) { rows, err := db.conn.Query(fmt.Sprintf(queryGetAllCSRs, db.table)) if err != nil { return nil, err @@ -44,7 +44,7 @@ func (db *CertificateRequests) RetrieveAll() ([]CertificateRequest, error) { return allCsrs, nil } -func (db *CertificateRequests) Retrieve(csr string) (*CertificateRequest, error) { +func (db *CertificateRequestsRepository) Retrieve(csr string) (*CertificateRequest, error) { var newCSR CertificateRequest row := db.conn.QueryRow(fmt.Sprintf(queryGetCSR, db.table), csr) if err := row.Scan(&newCSR.ID, &newCSR.CSR, &newCSR.Certificate); err != nil { @@ -53,7 +53,7 @@ func (db *CertificateRequests) Retrieve(csr string) (*CertificateRequest, error) return &newCSR, nil } -func (db *CertificateRequests) Create(csr string) (int64, error) { +func (db *CertificateRequestsRepository) Create(csr string) (int64, error) { if err := ValidateCertificateRequest(csr); err != nil { return 0, err } @@ -68,7 +68,7 @@ func (db *CertificateRequests) Create(csr string) (int64, error) { return id, nil } -func (db *CertificateRequests) Update(csr string, cert string) (int64, error) { +func (db *CertificateRequestsRepository) Update(csr string, cert string) (int64, error) { if err := ValidateCertificate(cert, csr); err != nil { return 0, err } @@ -83,7 +83,7 @@ func (db *CertificateRequests) Update(csr string, cert string) (int64, error) { return id, nil } -func (db *CertificateRequests) Delete(csr string) error { +func (db *CertificateRequestsRepository) Delete(csr string) error { _, err := db.conn.Exec(fmt.Sprintf(queryDeleteCSR, db.table), csr) if err != nil { return err @@ -91,7 +91,7 @@ func (db *CertificateRequests) Delete(csr string) error { return nil } -func (db *CertificateRequests) Connect(databasePath string, tableName string) error { +func (db *CertificateRequestsRepository) Connect(databasePath string, tableName string) error { conn, err := sql.Open("sqlite3", databasePath) if err != nil { return err @@ -104,7 +104,7 @@ func (db *CertificateRequests) Connect(databasePath string, tableName string) er return nil } -func (db *CertificateRequests) Disconnect() error { +func (db *CertificateRequestsRepository) Close() error { if db.conn == nil { return nil } diff --git a/internal/certdb/certdb_test.go b/internal/certdb/certdb_test.go index b043581..f937729 100644 --- a/internal/certdb/certdb_test.go +++ b/internal/certdb/certdb_test.go @@ -9,17 +9,17 @@ import ( ) func TestConnect(t *testing.T) { - db := new(certdb.CertificateRequests) - defer db.Disconnect() + db := new(certdb.CertificateRequestsRepository) + defer db.Close() if err := db.Connect(":memory:", "CertificateReqs"); err != nil { t.Fatalf("Can't connect to SQLite: %s", err) } } func TestEndToEnd(t *testing.T) { - db := new(certdb.CertificateRequests) - defer db.Disconnect() - db.Connect(":memory:", "CertificateRequests") + db := new(certdb.CertificateRequestsRepository) + defer db.Close() + db.Connect(":memory:", "CertificateRequests") //nolint:errcheck if _, err := db.Create(ValidCSR1); err != nil { t.Fatalf("Couldn't complete Create: %s", err) @@ -59,34 +59,34 @@ func TestEndToEnd(t *testing.T) { t.Fatalf("Couldn't complete Update: %s", err) } retrievedCSR, _ = db.Retrieve(ValidCSR2) - if *retrievedCSR.Certificate != ValidCert2 { - t.Fatalf("The certificate that was uploaded does not match the certificate that was given: Retrieved: %s\nGiven: %s", *retrievedCSR.Certificate, ValidCert2) + if retrievedCSR.Certificate != ValidCert2 { + t.Fatalf("The certificate that was uploaded does not match the certificate that was given: Retrieved: %s\nGiven: %s", retrievedCSR.Certificate, ValidCert2) } } func TestCreateFails(t *testing.T) { - db := new(certdb.CertificateRequests) - db.Connect(":memory:", "CertificateReqs") - defer db.Disconnect() + db := new(certdb.CertificateRequestsRepository) + db.Connect(":memory:", "CertificateReqs") //nolint:errcheck + defer db.Close() InvalidCSR := strings.ReplaceAll(ValidCSR1, "/", "+") if _, err := db.Create(InvalidCSR); err == nil { t.Fatalf("Expected error due to invalid CSR") } - db.Create(ValidCSR1) + db.Create(ValidCSR1) //nolint:errcheck if _, err := db.Create(ValidCSR1); err == nil { t.Fatalf("Expected error due to duplicate CSR") } } func TestUpdateFails(t *testing.T) { - db := new(certdb.CertificateRequests) - defer db.Disconnect() - db.Connect(":memory:", "CertificateRequests") + db := new(certdb.CertificateRequestsRepository) + defer db.Close() + db.Connect(":memory:", "CertificateRequests") //nolint:errcheck - db.Create(ValidCSR1) - db.Create(ValidCSR2) + db.Create(ValidCSR1) //nolint:errcheck + db.Create(ValidCSR2) //nolint:errcheck InvalidCert := strings.ReplaceAll(ValidCert2, "/", "+") if _, err := db.Update(ValidCSR2, InvalidCert); err == nil { t.Fatalf("Expected updating with invalid cert to fail") @@ -97,11 +97,11 @@ func TestUpdateFails(t *testing.T) { } func TestRetrieve(t *testing.T) { - db := new(certdb.CertificateRequests) - defer db.Disconnect() - db.Connect(":memory:", "CertificateRequests") + db := new(certdb.CertificateRequestsRepository) + defer db.Close() + db.Connect(":memory:", "CertificateRequests") //nolint:errcheck - db.Create(ValidCSR1) + db.Create(ValidCSR1) //nolint:errcheck if _, err := db.Retrieve(ValidCSR2); err == nil { t.Fatalf("Expected failure looking for nonexistent CSR") } @@ -109,9 +109,9 @@ func TestRetrieve(t *testing.T) { } func Example() { - db := new(certdb.CertificateRequests) + db := new(certdb.CertificateRequestsRepository) if err := db.Connect("./certs.db", "CertificateReq"); err != nil { log.Fatalln(err) } - defer db.Disconnect() + defer db.Close() }