github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/license_statuses/license_statuses_test.go (about)

     1  // Copyright 2020 Readium Foundation. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license
     3  // that can be found in the LICENSE file exposed on Github (readium) in the project repository.
     4  
     5  package licensestatuses
     6  
     7  import (
     8  	"database/sql"
     9  	"testing"
    10  	"time"
    11  
    12  	_ "github.com/mattn/go-sqlite3"
    13  
    14  	"github.com/readium/readium-lcp-server/config"
    15  )
    16  
    17  func TestCRUD(t *testing.T) {
    18  
    19  	config.Config.LsdServer.Database = "sqlite3://:memory:"
    20  	driver, cnxn := config.GetDatabase(config.Config.LsdServer.Database)
    21  	db, err := sql.Open(driver, cnxn)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	lst, err := Open(db)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	timestamp := time.Now().UTC().Truncate(time.Second)
    32  
    33  	// add
    34  	count := 2
    35  	ls := LicenseStatus{PotentialRights: &PotentialRights{End: &timestamp}, LicenseRef: "licenseref", Status: "active", Updated: &Updated{License: &timestamp, Status: &timestamp}, DeviceCount: &count}
    36  	err = lst.Add(ls)
    37  	if err != nil {
    38  		t.Error(err)
    39  	}
    40  
    41  	// list with no device limit, 10 max, offset 0
    42  	fn := lst.List(0, 10, 0)
    43  	if fn == nil {
    44  		t.Errorf("Failed getting a non null list function")
    45  	}
    46  	statusList := make([]LicenseStatus, 0)
    47  	var it LicenseStatus
    48  	for it, err = fn(); err == nil; it, err = fn() {
    49  		statusList = append(statusList, it)
    50  	}
    51  	if err != ErrNotFound {
    52  		t.Error(err)
    53  	}
    54  	if len(statusList) != 1 {
    55  		t.Errorf("Failed getting a list with one item, got %d instead", len(statusList))
    56  	}
    57  
    58  	// get by id
    59  	statusID := statusList[0].ID
    60  	_, err = lst.GetByID(statusID)
    61  	if err != nil {
    62  		t.Error(err)
    63  	}
    64  
    65  	// get by license id
    66  	ls2, err := lst.GetByLicenseID("licenseref")
    67  	if err != nil {
    68  		t.Error(err)
    69  	}
    70  	if ls2.ID != statusID {
    71  		t.Errorf("Failed getting a license status by license id")
    72  	}
    73  
    74  	// update
    75  	ls2.Status = "revoked"
    76  	err = lst.Update(*ls2)
    77  	if err != nil {
    78  		t.Error(err)
    79  	}
    80  
    81  	ls3, err := lst.GetByID(ls2.ID)
    82  	if err != nil {
    83  		t.Error(err)
    84  	}
    85  	if ls3.Status != "revoked" {
    86  		t.Errorf("Failed getting the proper stats, got %s instead", ls3.Status)
    87  	}
    88  
    89  }