-
-
Notifications
You must be signed in to change notification settings - Fork 58
test: add tests for unprotected behavior #2203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nilsreichardt
wants to merge
2
commits into
main
Choose a base branch
from
add-unprotected-behavior-tests-13501131065073978994
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+690
−39
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,84 @@ | ||
| // Copyright (c) 2026 Sharezone UG (haftungsbeschränkt) | ||
| // Licensed under the EUPL-1.2-or-later. | ||
| // | ||
| // You may obtain a copy of the Licence at: | ||
| // https://joinup.ec.europa.eu/software/page/eupl | ||
| // | ||
| // SPDX-License-Identifier: EUPL-1.2 | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:group_domain_models/group_domain_models.dart'; | ||
| import 'package:sharezone/groups/group_permission.dart'; | ||
|
|
||
| void main() { | ||
| group('HasRolePermission', () { | ||
| group('hasPermission for administration', () { | ||
| test('should be true for owner', () { | ||
| expect(MemberRole.owner.hasPermission(GroupPermission.administration), | ||
| isTrue); | ||
| }); | ||
|
|
||
| test('should be true for admin', () { | ||
| expect(MemberRole.admin.hasPermission(GroupPermission.administration), | ||
| isTrue); | ||
| }); | ||
|
|
||
| test('should be false for creator', () { | ||
| expect(MemberRole.creator.hasPermission(GroupPermission.administration), | ||
| isFalse); | ||
| }); | ||
|
|
||
| test('should be false for standard', () { | ||
| expect( | ||
| MemberRole.standard.hasPermission(GroupPermission.administration), | ||
| isFalse); | ||
| }); | ||
| }); | ||
|
|
||
| group('hasPermission for contentCreation', () { | ||
| test('should be true for owner', () { | ||
| expect(MemberRole.owner.hasPermission(GroupPermission.contentCreation), | ||
| isTrue); | ||
| }); | ||
|
|
||
| test('should be true for admin', () { | ||
| expect(MemberRole.admin.hasPermission(GroupPermission.contentCreation), | ||
| isTrue); | ||
| }); | ||
|
|
||
| test('should be true for creator', () { | ||
| expect( | ||
| MemberRole.creator.hasPermission(GroupPermission.contentCreation), | ||
| isTrue); | ||
| }); | ||
|
|
||
| test('should be false for standard', () { | ||
| expect( | ||
| MemberRole.standard.hasPermission(GroupPermission.contentCreation), | ||
| isFalse); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| group('isUserAdminOrOwnerOfGroup', () { | ||
| test('should be true for owner', () { | ||
| expect(isUserAdminOrOwnerOfGroup(MemberRole.owner), isTrue); | ||
| }); | ||
|
|
||
| test('should be true for admin', () { | ||
| expect(isUserAdminOrOwnerOfGroup(MemberRole.admin), isTrue); | ||
| }); | ||
|
|
||
| test('should be false for creator', () { | ||
| expect(isUserAdminOrOwnerOfGroup(MemberRole.creator), isFalse); | ||
| }); | ||
|
|
||
| test('should be false for standard', () { | ||
| expect(isUserAdminOrOwnerOfGroup(MemberRole.standard), isFalse); | ||
| }); | ||
|
|
||
| test('should be false for null role', () { | ||
| expect(isUserAdminOrOwnerOfGroup(null), isFalse); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or 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,93 @@ | ||
| // Copyright (c) 2026 Sharezone UG (haftungsbeschränkt) | ||
| // Licensed under the EUPL-1.2-or-later. | ||
| // | ||
| // You may obtain a copy of the Licence at: | ||
| // https://joinup.ec.europa.eu/software/page/eupl | ||
| // | ||
| // SPDX-License-Identifier: EUPL-1.2 | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:group_domain_models/group_domain_models.dart'; | ||
| import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik.dart'; | ||
| import 'package:mockito/annotations.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:sharezone/submissions/submission_permissions.dart'; | ||
| import 'package:sharezone/util/api/course_gateway.dart'; | ||
| import 'package:user/user.dart'; | ||
|
|
||
| @GenerateNiceMocks([MockSpec<CourseGateway>()]) | ||
| import 'submission_permissions_test.mocks.dart'; | ||
|
|
||
| void main() { | ||
| late MockCourseGateway mockCourseGateway; | ||
|
|
||
| setUp(() { | ||
| mockCourseGateway = MockCourseGateway(); | ||
| }); | ||
|
|
||
| group('SubmissionPermissions.isAllowedToViewSubmittedPermissions', () { | ||
| test('should allow when user is teacher and is admin in course', () async { | ||
| when(mockCourseGateway.getRoleFromCourseNoSync(any)) | ||
| .thenReturn(MemberRole.admin); | ||
| final stream = Stream.value(TypeOfUser.teacher); | ||
| final permissions = SubmissionPermissions(stream, mockCourseGateway); | ||
| final homework = HomeworkDto.create(courseID: 'course1'); | ||
|
|
||
| final result = | ||
| await permissions.isAllowedToViewSubmittedPermissions(homework); | ||
|
|
||
| expect(result, isTrue); | ||
| }); | ||
|
|
||
| test('should allow when user is teacher and is owner in course', () async { | ||
| when(mockCourseGateway.getRoleFromCourseNoSync(any)) | ||
| .thenReturn(MemberRole.owner); | ||
| final stream = Stream.value(TypeOfUser.teacher); | ||
| final permissions = SubmissionPermissions(stream, mockCourseGateway); | ||
| final homework = HomeworkDto.create(courseID: 'course1'); | ||
|
|
||
| final result = | ||
| await permissions.isAllowedToViewSubmittedPermissions(homework); | ||
|
|
||
| expect(result, isTrue); | ||
| }); | ||
|
|
||
| test('should deny when user is teacher but only standard in course', () async { | ||
| when(mockCourseGateway.getRoleFromCourseNoSync(any)) | ||
| .thenReturn(MemberRole.standard); | ||
| final stream = Stream.value(TypeOfUser.teacher); | ||
| final permissions = SubmissionPermissions(stream, mockCourseGateway); | ||
| final homework = HomeworkDto.create(courseID: 'course1'); | ||
|
|
||
| final result = | ||
| await permissions.isAllowedToViewSubmittedPermissions(homework); | ||
|
|
||
| expect(result, isFalse); | ||
| }); | ||
|
|
||
| test('should deny when user is student even if admin in course', () async { | ||
| when(mockCourseGateway.getRoleFromCourseNoSync(any)) | ||
| .thenReturn(MemberRole.admin); | ||
| final stream = Stream.value(TypeOfUser.student); | ||
| final permissions = SubmissionPermissions(stream, mockCourseGateway); | ||
| final homework = HomeworkDto.create(courseID: 'course1'); | ||
|
|
||
| final result = | ||
| await permissions.isAllowedToViewSubmittedPermissions(homework); | ||
|
|
||
| expect(result, isFalse); | ||
| }); | ||
|
|
||
| test('should deny when course role is null (defaults to standard)', () async { | ||
| when(mockCourseGateway.getRoleFromCourseNoSync(any)).thenReturn(null); | ||
| final stream = Stream.value(TypeOfUser.teacher); | ||
| final permissions = SubmissionPermissions(stream, mockCourseGateway); | ||
| final homework = HomeworkDto.create(courseID: 'course1'); | ||
|
|
||
| final result = | ||
| await permissions.isAllowedToViewSubmittedPermissions(homework); | ||
|
|
||
| expect(result, isFalse); | ||
| }); | ||
| }); | ||
|
Comment on lines
+28
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests in this group are very similar and contain duplicated code. You can refactor this using a data-driven approach to make the tests more concise and easier to maintain and extend. group('SubmissionPermissions.isAllowedToViewSubmittedPermissions', () {
final homework = HomeworkDto.create(courseID: 'course1');
final testCases = <String, ({TypeOfUser userType, MemberRole? role, bool expected})>{
'allow when user is teacher and is admin in course': (
userType: TypeOfUser.teacher,
role: MemberRole.admin,
expected: true,
),
'allow when user is teacher and is owner in course': (
userType: TypeOfUser.teacher,
role: MemberRole.owner,
expected: true,
),
'deny when user is teacher but only standard in course': (
userType: TypeOfUser.teacher,
role: MemberRole.standard,
expected: false,
),
'deny when user is student even if admin in course': (
userType: TypeOfUser.student,
role: MemberRole.admin,
expected: false,
),
'deny when course role is null (defaults to standard)': (
userType: TypeOfUser.teacher,
role: null,
expected: false,
),
};
for (final entry in testCases.entries) {
test('should ${entry.key}', () async {
when(mockCourseGateway.getRoleFromCourseNoSync(any))
.thenReturn(entry.value.role);
final stream = Stream.value(entry.value.userType);
final permissions = SubmissionPermissions(stream, mockCourseGateway);
final result =
await permissions.isAllowedToViewSubmittedPermissions(homework);
expect(result, entry.value.expected);
});
}
}); |
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests in these groups are repetitive. You can use a data-driven approach with a map of test cases to make the code more concise and maintainable. This pattern can also be applied to the
isUserAdminOrOwnerOfGrouptest group.