github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/web/groups_name_test.go (about)

     1  package web_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"connectrpc.com/connect"
     8  	"github.com/quickfeed/quickfeed/internal/qtest"
     9  	"github.com/quickfeed/quickfeed/qf"
    10  	"github.com/quickfeed/quickfeed/web"
    11  )
    12  
    13  func TestBadGroupNames(t *testing.T) {
    14  	db, cleanup := qtest.TestDB(t)
    15  	defer cleanup()
    16  
    17  	admin := qtest.CreateFakeUser(t, db)
    18  	course := &qf.Course{
    19  		Name: "Distributed Systems",
    20  		Code: "DAT520",
    21  		Year: 2018,
    22  	}
    23  	qtest.CreateCourse(t, db, admin, course)
    24  
    25  	client := MockClient(t, db, nil)
    26  	groupNames := []struct {
    27  		name      string
    28  		wantError *connect.Error
    29  	}{
    30  		{"abcdefghijklmnopqrstuvwxyz", web.ErrGroupNameTooLong},
    31  		{"groupNameStillTooLong", web.ErrGroupNameTooLong},
    32  		{"groupNameNotTooLong", nil},
    33  		{"a", nil},
    34  		{"a1", nil},
    35  		{"23", nil},
    36  		{"HeinsGroup", nil},
    37  		{"Heins-group", nil},
    38  		{"Heins_group", nil},
    39  		{"Hein's group", web.ErrGroupNameInvalid},
    40  		{"a" + string([]byte{0x7f}), web.ErrGroupNameInvalid},
    41  		{"abc ", web.ErrGroupNameInvalid},
    42  		{"æ", web.ErrGroupNameInvalid},
    43  		{"ø", web.ErrGroupNameInvalid},
    44  		{"å", web.ErrGroupNameInvalid},
    45  		{"Æ", web.ErrGroupNameInvalid},
    46  		{"Ø", web.ErrGroupNameInvalid},
    47  		{"Å", web.ErrGroupNameInvalid},
    48  		{"DuplicateGroupName", nil},
    49  		{"DuplicateGroupName", web.ErrGroupNameDuplicate},
    50  		{"duplicateGroupName", web.ErrGroupNameDuplicate},
    51  		{"duplicateGroupname", web.ErrGroupNameDuplicate},
    52  	}
    53  	for _, tt := range groupNames {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			user1 := qtest.CreateFakeUser(t, db)
    56  			user2 := qtest.CreateFakeUser(t, db)
    57  			qtest.EnrollStudent(t, db, user1, course)
    58  			qtest.EnrollStudent(t, db, user2, course)
    59  
    60  			group := &qf.Group{
    61  				CourseID: course.ID,
    62  				Name:     tt.name,
    63  				Users:    []*qf.User{user1, user2},
    64  			}
    65  			_, err := client.CreateGroup(context.Background(), connect.NewRequest(group))
    66  			if tt.wantError == nil {
    67  				if err != nil {
    68  					t.Errorf("got error %v, want nil", err)
    69  				}
    70  				return
    71  			}
    72  			if err == nil && tt.wantError != nil {
    73  				t.Errorf("got no error, want %v", tt.wantError)
    74  			}
    75  			if connErr, ok := err.(*connect.Error); ok {
    76  				if connErr.Code() != tt.wantError.Code() {
    77  					t.Errorf("got error code %v, want %v", connErr.Code(), tt.wantError.Code())
    78  				}
    79  				if connErr.Error() != tt.wantError.Error() {
    80  					t.Errorf("got error %v, want %v", connErr, tt.wantError)
    81  				}
    82  			}
    83  		})
    84  	}
    85  }