-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix class; Update letterBytes for random string
- Loading branch information
Showing
6 changed files
with
68 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/leoleoasd/EduOJBackend/base" | ||
"github.com/leoleoasd/EduOJBackend/database/models" | ||
"sync" | ||
) | ||
|
||
var inviteCodeLock sync.Mutex | ||
|
||
func GenerateInviteCode() (code string) { | ||
inviteCodeLock.Lock() | ||
defer inviteCodeLock.Unlock() | ||
var count int64 = 1 | ||
for count > 0 { | ||
// 5: Fixed invite code length | ||
code = RandStr(5) | ||
PanicIfDBError(base.DB.Model(models.Class{}).Where("invite_code = ?", code).Count(&count), | ||
"could not check if invite code crashed for generating invite code") | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"github.com/leoleoasd/EduOJBackend/base" | ||
"github.com/leoleoasd/EduOJBackend/database/models" | ||
"github.com/stretchr/testify/assert" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func checkInviteCode(t *testing.T, code string) { | ||
assert.Regexp(t, regexp.MustCompile("^[a-zA-Z2-9]{5}$"), code) | ||
var count int64 | ||
assert.NoError(t, base.DB.Model(models.Class{}).Where("invite_code = ?", code).Count(&count).Error) | ||
assert.Equal(t, int64(1), count) | ||
} | ||
|
||
func createClassForTest(t *testing.T, name string, id int, managers, students []models.User) models.Class { | ||
inviteCode := GenerateInviteCode() | ||
class := models.Class{ | ||
Name: fmt.Sprintf("test_%s_%d_name", name, id), | ||
CourseName: fmt.Sprintf("test_%s_%d_course_name", name, id), | ||
Description: fmt.Sprintf("test_%s_%d_description", name, id), | ||
InviteCode: inviteCode, | ||
Managers: managers, | ||
Students: students, | ||
} | ||
assert.NoError(t, base.DB.Create(&class).Error) | ||
return class | ||
} | ||
|
||
func TestGenerateInviteCode(t *testing.T) { | ||
t.Parallel() | ||
class := createClassForTest(t, "test_generate_invite_code_success", 0, nil, nil) | ||
checkInviteCode(t, class.InviteCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters