forked from edulinq/autograder-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Role Based Addressing Functionality to Report Field and Send Em…
…ail Command (edulinq#66)
- Loading branch information
1 parent
2b8257c
commit dcc7c26
Showing
5 changed files
with
271 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package db | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/edulinq/autograder/email" | ||
"github.com/edulinq/autograder/log" | ||
"github.com/edulinq/autograder/model" | ||
"github.com/edulinq/autograder/util" | ||
) | ||
|
@@ -14,6 +17,180 @@ type SyncNewUsersTestCase struct { | |
sendEmails bool | ||
} | ||
|
||
func (this *DBTests) DBTestResolveUsers(test *testing.T) { | ||
defer ResetForTesting(); | ||
|
||
oldValue := log.SetBackgroundLogging(false); | ||
defer log.SetBackgroundLogging(oldValue); | ||
|
||
log.SetLevels(log.LevelOff, log.LevelWarn); | ||
defer log.SetLevelFatal(); | ||
|
||
// Wait for old logs to get written. | ||
time.Sleep(10 * time.Millisecond); | ||
|
||
Clear(); | ||
defer Clear(); | ||
|
||
testCases := []struct {input []string; expectedOutput []string; addUsers []*model.User; removeUsers []string; numWarnings int} { | ||
// This test case tests the empty slice input. | ||
{ | ||
[]string{}, | ||
[]string{}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This is a simple test case for the empty string input. | ||
{ | ||
[]string{""}, | ||
[]string{}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This is a test to ensure the output is sorted. | ||
{ | ||
[]string{"[email protected]", "[email protected]", "[email protected]"}, | ||
[]string{"[email protected]", "[email protected]", "[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This is a test to ensure miscapitalized emails only get returned once. | ||
{ | ||
[]string{"[email protected]", "[email protected]", "[email protected]"}, | ||
[]string{"[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This is a basic test to ensure that a role gets mapped to the correct email. | ||
{ | ||
[]string{"admin"}, | ||
[]string{"[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This is a test for our all roles character, the *. | ||
{ | ||
[]string{"*"}, | ||
[]string{"[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
|
||
// This test case is given redundant roles and emails. | ||
// It tests to ensures we do not produce duplicates on this input. | ||
{ | ||
[]string{"other", "*", "[email protected]"}, | ||
[]string{"[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This test case tests if miscapitalized roles still function. | ||
{ | ||
[]string{"OTHER"}, | ||
[]string{"[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This test case tests if warnings are issued on invalid roles. | ||
{ | ||
[]string{"trash", "garbage", "waste", "recycle!"}, | ||
[]string{}, | ||
nil, | ||
[]string{}, | ||
4, | ||
}, | ||
|
||
// This test adds new Users to the course and ensures we retrieve all emails for the given role. | ||
{ | ||
[]string{"student"}, | ||
[]string{"[email protected]", "[email protected]", "[email protected]"}, | ||
[]*model.User{model.NewUser("[email protected]", "", model.GetRole("student")), model.NewUser("[email protected]", "", model.GetRole("student"))}, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This is a test case to see if we properly trim whitespace. | ||
{ | ||
[]string{"\t\n student ", "\n \t [email protected]", "\t\n \t \n"}, | ||
[]string{"[email protected]", "[email protected]"}, | ||
nil, | ||
[]string{}, | ||
0, | ||
}, | ||
|
||
// This test case removes the only user from the "owner" role, so we check that a role without any users still functions properly. | ||
{ | ||
[]string{"owner", "student"}, | ||
[]string{"[email protected]"}, | ||
nil, | ||
[]string{"[email protected]"}, | ||
0, | ||
}, | ||
|
||
// This test supplies a single role that resolves to nothing. | ||
{ | ||
[]string{"owner"}, | ||
[]string{}, | ||
nil, | ||
[]string{"[email protected]"}, | ||
0, | ||
}, | ||
}; | ||
|
||
for i, testCase := range testCases { | ||
ResetForTesting(); | ||
course := MustGetCourse(TEST_COURSE_ID); | ||
|
||
for _, newUser := range testCase.addUsers { | ||
SaveUser(course, newUser); | ||
} | ||
|
||
for _, removeUser := range testCase.removeUsers { | ||
RemoveUser(course, removeUser); | ||
} | ||
|
||
actualOutput, err := ResolveUsers(course, testCase.input); | ||
if (err != nil) { | ||
test.Errorf("Case %d (%+v): Resolve User failed: '%v'.", i, testCase, err); | ||
continue; | ||
} | ||
|
||
if (!reflect.DeepEqual(testCase.expectedOutput, actualOutput)) { | ||
test.Errorf("Case %d (%+v): Incorrect Output. Expected: '%v', Actual: '%v'.", i, | ||
testCase, testCase.expectedOutput, actualOutput); | ||
continue; | ||
} | ||
|
||
logs, err := GetLogRecords(log.LevelWarn, time.Time{}, "", "", "") | ||
if (err != nil) { | ||
test.Errorf("Case %d (%+v): Error getting log records.", i, testCase); | ||
continue; | ||
} | ||
|
||
if (testCase.numWarnings != len(logs)) { | ||
test.Errorf("Case %d (%+v): Incorrect number of warnings issued. Expected: %d, Actual: %d.", i, | ||
testCase, testCase.numWarnings, len(logs)); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
func (this *DBTests) DBTestCourseSyncNewUsers(test *testing.T) { | ||
defer ResetForTesting(); | ||
|
||
|
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