@@ -52,28 +52,54 @@ def test_default_authenticator():
5252
5353
5454def test_jupyterhub_get_student_courses (env , jupyterhub_auth ):
55- # this will fail, because the api token hasn't been set
55+ # this will fail, because the user hasn't been set
56+ env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
57+ env ['JUPYTERHUB_USER' ] = ''
5658 with pytest .raises (JupyterhubEnvironmentError ):
5759 jupyterhub_auth .get_student_courses ('foo' )
5860
59- # should still fail, because the user hasn't been set
60- env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
61+ # this will fail, because the api token hasn't been set
62+ env ['JUPYTERHUB_API_TOKEN' ] = ''
63+ env ['JUPYTERHUB_USER' ] = 'foo'
6164 with pytest .raises (JupyterhubEnvironmentError ):
6265 jupyterhub_auth .get_student_courses ('foo' )
6366
6467 # should fail, because the server returns a forbidden response
68+ env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
6569 env ['JUPYTERHUB_USER' ] = 'foo'
6670 with requests_mock .Mocker () as m :
6771 _mock_api_call (m .get , '/users/foo' , status_code = 403 )
6872 with pytest .raises (JupyterhubApiError ):
6973 jupyterhub_auth .get_student_courses ('foo' )
7074
71- # should succeed
75+ # should succeed, but give no courses, because the group name is invalid
7276 with requests_mock .Mocker () as m :
77+ _mock_api_call (m .get , '/users/foo' , json = {'groups' : ['nbgrader-' ]})
78+ assert jupyterhub_auth .get_student_courses ('foo' ) == []
79+
80+ _mock_api_call (m .get , '/users/foo' , json = {'groups' : ['course101' ]})
81+ assert jupyterhub_auth .get_student_courses ('foo' ) == []
82+
7383 _mock_api_call (
7484 m .get , '/users/foo' , json = {'groups' : ['nbgrader-course123' ]})
7585 assert jupyterhub_auth .get_student_courses ('foo' ) == ['course123' ]
7686
87+ # should succeed
88+ with requests_mock .Mocker () as m :
89+ _mock_api_call (
90+ m .get , '/users/foo' , json = {'groups' : ['nbgrader-course123' ]})
91+ assert jupyterhub_auth .get_student_courses ('*' ) == ['course123' ]
92+
93+
94+ def test_jupyterhub_has_access (env , jupyterhub_auth ):
95+ env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
96+ env ['JUPYTERHUB_USER' ] = 'foo'
97+ with requests_mock .Mocker () as m :
98+ _mock_api_call (
99+ m .get , '/users/foo' , json = {'groups' : ['nbgrader-course123' ]})
100+ assert jupyterhub_auth .has_access ('foo' , 'course123' )
101+ assert not jupyterhub_auth .has_access ('foo' , 'courseABC' )
102+
77103
78104def test_jupyterhub_add_student_to_course_no_token (jupyterhub_auth ):
79105 # this will fail, because the api token hasn't been set
@@ -98,6 +124,16 @@ def test_jupyterhub_add_student_to_course_forbidden(env, jupyterhub_auth, caplog
98124 assert 'ERROR' in [rec .levelname for rec in caplog .records ]
99125
100126
127+ def test_jupyterhub_add_student_to_course_no_courseid (env , jupyterhub_auth , caplog ):
128+ # test case where something goes wrong
129+ env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
130+ env ['JUPYTERHUB_USER' ] = 'foo'
131+ with requests_mock .Mocker () as m :
132+ _mock_api_call (m .get , '/groups' , status_code = 403 )
133+ jupyterhub_auth .add_student_to_course ('foo' , None )
134+ assert 'ERROR' in [rec .levelname for rec in caplog .records ]
135+
136+
101137def test_jupyterhub_add_student_to_course_success (env , jupyterhub_auth , caplog ):
102138 env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
103139 env ['JUPYTERHUB_USER' ] = 'foo'
@@ -108,6 +144,12 @@ def test_jupyterhub_add_student_to_course_success(env, jupyterhub_auth, caplog):
108144 jupyterhub_auth .add_student_to_course ('foo' , 'course123' )
109145 assert 'ERROR' not in [rec .levelname for rec in caplog .records ]
110146
147+ # Check that it also works if the group already exists
148+ _mock_api_call (m .get , '/groups' , json = [{'name' : 'nbgrader-course123' }])
149+ _mock_api_call (m .post , '/groups/nbgrader-course123/users' )
150+ jupyterhub_auth .add_student_to_course ('foo' , 'course123' )
151+ assert 'ERROR' not in [rec .levelname for rec in caplog .records ]
152+
111153
112154def test_jupyterhub_remove_student_from_course_no_token (jupyterhub_auth ):
113155 # this will fail, because the api token hasn't been set
@@ -132,6 +174,16 @@ def test_jupyterhub_remove_student_from_course_forbidden(env, jupyterhub_auth, c
132174 assert 'ERROR' in [rec .levelname for rec in caplog .records ]
133175
134176
177+ def test_jupyterhub_remove_student_from_course_no_courseid (env , jupyterhub_auth , caplog ):
178+ # test case where something goes wrong
179+ env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
180+ env ['JUPYTERHUB_USER' ] = 'foo'
181+ with requests_mock .Mocker () as m :
182+ _mock_api_call (m .delete , '/groups/nbgrader-course123/users' )
183+ jupyterhub_auth .remove_student_from_course ('foo' , None )
184+ assert 'ERROR' in [rec .levelname for rec in caplog .records ]
185+
186+
135187def test_jupyterhub_remove_student_from_course_success (env , jupyterhub_auth , caplog ):
136188 env ['JUPYTERHUB_API_TOKEN' ] = 'abcd1234'
137189 env ['JUPYTERHUB_USER' ] = 'foo'
0 commit comments