Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Apr 1, 2024
1 parent efdfbfd commit 6b77d1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
29 changes: 16 additions & 13 deletions internal/certdb/certdb.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package certdb provides a simplistic ORM to communicate with an SQL database for storage
package certdb

import (
Expand All @@ -15,6 +16,7 @@ const queryCreateCSR = "INSERT INTO %s (CSR) VALUES (?)"
const queryUpdateCSR = "UPDATE %s SET Certificate=? WHERE CSR=?"
const queryDeleteCSR = "DELETE FROM %s WHERE CSR=?"

// CertificateRequestRepository is the object used to communicate with the established repository.
type CertificateRequestsRepository struct {
table string
conn *sql.DB
Expand Down Expand Up @@ -91,19 +93,6 @@ func (db *CertificateRequestsRepository) Delete(csr string) error {
return nil
}

func (db *CertificateRequestsRepository) Connect(databasePath string, tableName string) error {
conn, err := sql.Open("sqlite3", databasePath)
if err != nil {
return err
}
db.table = tableName
db.conn = conn
if _, err := db.conn.Exec(fmt.Sprintf(queryCreateTable, db.table)); err != nil {
return err
}
return nil
}

func (db *CertificateRequestsRepository) Close() error {
if db.conn == nil {
return nil
Expand All @@ -113,3 +102,17 @@ func (db *CertificateRequestsRepository) Close() error {
}
return nil
}

func NewCertificateRequestsRepository(databasePath string, tableName string) (*CertificateRequestsRepository, error) {
conn, err := sql.Open("sqlite3", databasePath)
if err != nil {
return nil, err
}
if _, err := conn.Exec(fmt.Sprintf(queryCreateTable, tableName)); err != nil {
return nil, err
}
db := new(CertificateRequestsRepository)
db.conn = conn
db.table = tableName
return db, nil
}
40 changes: 27 additions & 13 deletions internal/certdb/certdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import (
)

func TestConnect(t *testing.T) {
db := new(certdb.CertificateRequestsRepository)
defer db.Close()
if err := db.Connect(":memory:", "CertificateReqs"); err != nil {
db, err := certdb.NewCertificateRequestsRepository(":memory:", "CertificateReqs")
if err != nil {
t.Fatalf("Can't connect to SQLite: %s", err)
}
db.Close()
}

func TestEndToEnd(t *testing.T) {
db := new(certdb.CertificateRequestsRepository)
db, err := certdb.NewCertificateRequestsRepository(":memory:", "CertificateRequests") //nolint:errcheck
if err != nil {
t.Fatalf("Couldn't complete NewCertificateRequestsRepository: %s", err)
}
defer db.Close()
db.Connect(":memory:", "CertificateRequests") //nolint:errcheck

if _, err := db.Create(ValidCSR1); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
Expand Down Expand Up @@ -65,8 +67,7 @@ func TestEndToEnd(t *testing.T) {
}

func TestCreateFails(t *testing.T) {
db := new(certdb.CertificateRequestsRepository)
db.Connect(":memory:", "CertificateReqs") //nolint:errcheck
db, _ := certdb.NewCertificateRequestsRepository(":memory:", "CertificateReqs") //nolint:errcheck
defer db.Close()

InvalidCSR := strings.ReplaceAll(ValidCSR1, "/", "+")
Expand All @@ -81,9 +82,8 @@ func TestCreateFails(t *testing.T) {
}

func TestUpdateFails(t *testing.T) {
db := new(certdb.CertificateRequestsRepository)
db, _ := certdb.NewCertificateRequestsRepository(":memory:", "CertificateRequests") //nolint:errcheck
defer db.Close()
db.Connect(":memory:", "CertificateRequests") //nolint:errcheck

db.Create(ValidCSR1) //nolint:errcheck
db.Create(ValidCSR2) //nolint:errcheck
Expand All @@ -97,9 +97,8 @@ func TestUpdateFails(t *testing.T) {
}

func TestRetrieve(t *testing.T) {
db := new(certdb.CertificateRequestsRepository)
db, _ := certdb.NewCertificateRequestsRepository(":memory:", "CertificateRequests") //nolint:errcheck
defer db.Close()
db.Connect(":memory:", "CertificateRequests") //nolint:errcheck

db.Create(ValidCSR1) //nolint:errcheck
if _, err := db.Retrieve(ValidCSR2); err == nil {
Expand All @@ -109,9 +108,24 @@ func TestRetrieve(t *testing.T) {
}

func Example() {
db := new(certdb.CertificateRequestsRepository)
if err := db.Connect("./certs.db", "CertificateReq"); err != nil {
db, err := certdb.NewCertificateRequestsRepository("./certs.db", "CertificateReq")
if err != nil {
log.Fatalln(err)
}
_, err = db.Create(ValidCSR2)
if err != nil {
log.Fatalln(err)
}
_, err = db.Update(ValidCSR2, ValidCert2)
if err != nil {
log.Fatalln(err)
}
entry, err := db.Retrieve(ValidCSR2)
if err != nil {
log.Fatalln(err)
}
if entry.Certificate != ValidCert2 {
log.Fatalln("Retrieved Certificate doesn't match Stored Certificate")
}
defer db.Close()
}

0 comments on commit 6b77d1b

Please sign in to comment.