github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/internal/qtest/test_helper.go (about)

     1  package qtest
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/sha256"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  
    10  	"connectrpc.com/connect"
    11  	"github.com/quickfeed/quickfeed/database"
    12  	"github.com/quickfeed/quickfeed/qf"
    13  )
    14  
    15  // TestDB returns a test database and close function.
    16  // This function should only be used as a test helper.
    17  func TestDB(t *testing.T) (database.Database, func()) {
    18  	t.Helper()
    19  
    20  	f, err := os.CreateTemp(t.TempDir(), "test.db")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	if err := f.Close(); err != nil {
    25  		os.Remove(f.Name())
    26  		t.Fatal(err)
    27  	}
    28  
    29  	db, err := database.NewGormDB(f.Name(), Logger(t).Desugar())
    30  	if err != nil {
    31  		os.Remove(f.Name())
    32  		t.Fatal(err)
    33  	}
    34  
    35  	return db, func() {
    36  		if err := db.Close(); err != nil {
    37  			t.Error(err)
    38  		}
    39  		if err := os.Remove(f.Name()); err != nil {
    40  			t.Error(err)
    41  		}
    42  	}
    43  }
    44  
    45  // CreateFakeUser is a test helper to create a user in the database.
    46  func CreateFakeUser(t *testing.T, db database.Database) *qf.User {
    47  	t.Helper()
    48  	user := &qf.User{}
    49  	if err := db.CreateUser(user); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	return user
    53  }
    54  
    55  func CreateFakeCustomUser(t *testing.T, db database.Database, user *qf.User) *qf.User {
    56  	t.Helper()
    57  	if err := db.CreateUser(user); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	return user
    61  }
    62  
    63  func CreateCourse(t *testing.T, db database.Database, user *qf.User, course *qf.Course) {
    64  	t.Helper()
    65  	if err := db.CreateCourse(user.ID, course); err != nil {
    66  		t.Fatal(err)
    67  	}
    68  }
    69  
    70  func EnrollStudent(t *testing.T, db database.Database, student *qf.User, course *qf.Course) {
    71  	t.Helper()
    72  	query := &qf.Enrollment{
    73  		UserID:   student.ID,
    74  		CourseID: course.ID,
    75  	}
    76  	if err := db.CreateEnrollment(query); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	query.Status = qf.Enrollment_STUDENT
    80  	if err := db.UpdateEnrollment(query); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  }
    84  
    85  func EnrollTeacher(t *testing.T, db database.Database, student *qf.User, course *qf.Course) {
    86  	t.Helper()
    87  	query := &qf.Enrollment{
    88  		UserID:   student.ID,
    89  		CourseID: course.ID,
    90  	}
    91  	if err := db.CreateEnrollment(query); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	query.Status = qf.Enrollment_TEACHER
    95  	if err := db.UpdateEnrollment(query); err != nil {
    96  		t.Fatal(err)
    97  	}
    98  }
    99  
   100  func EnrollUser(t *testing.T, db database.Database, user *qf.User, course *qf.Course, status qf.Enrollment_UserStatus) *qf.Enrollment {
   101  	t.Helper()
   102  	enrollment := &qf.Enrollment{
   103  		UserID:   user.ID,
   104  		CourseID: course.ID,
   105  	}
   106  	if err := db.CreateEnrollment(enrollment); err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	enrollment.Status = status
   110  	if err := db.UpdateEnrollment(enrollment); err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	return enrollment
   114  }
   115  
   116  func RandomString(t *testing.T) string {
   117  	t.Helper()
   118  	randomness := make([]byte, 10)
   119  	if _, err := rand.Read(randomness); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	return fmt.Sprintf("%x", sha256.Sum256(randomness))[:6]
   123  }
   124  
   125  func RequestWithCookie[T any](message *T, cookie string) *connect.Request[T] {
   126  	request := connect.NewRequest(message)
   127  	request.Header().Set("cookie", cookie)
   128  	return request
   129  }