def test_validate_ticket_invalid_response(self, mock_urlopen): self.response.read.return_value = \ "holy crap! I'm not a valid CAS response!" mock_urlopen.return_value = self.response self.assertEqual( validate_cas2_ticket( "foo", "https://slank.ccnmtl.columbia.edu/accounts/caslogin/?next=/"), (False, "CAS did not return a valid response.", [])) mock_urlopen.assert_called_with( "https://cas.columbia.edu/cas/serviceValidate?ticket=foo" "&service=https%3A//slank.ccnmtl.columbia.edu/accounts/" "caslogin/%3Fnext%3D/")
def test_validate_ticket_success(self, mock_urlopen): self.response.read.return_value = tr_affils() mock_urlopen.return_value = self.response with self.settings(CAS_BASE="https://cas.example.com/"): self.assertEqual( validate_cas2_ticket( "foo", ("https://slank.ccnmtl.columbia.edu/" "accounts/caslogin/?next=/")), (True, "test_claim", ["test_claim", "crs-3", "crs-1"])) mock_urlopen.assert_called_with( "https://cas.example.com/cas/serviceValidate?" "ticket=foo&service=https%3A//" "slank.ccnmtl.columbia.edu/accounts/caslogin/%3Fnext%3D/")
def test_validate_tr_success(self, mock_urlopen): """ for teachrecovery, we authenticate against a drupal CAS server. The documented response looks like this: https://gist.github.com/cravecode/6679b68d14a7250c8fe9 """ self.response.read.return_value = TR_SUCCESS mock_urlopen.return_value = self.response self.assertEqual( validate_cas2_ticket( "foo", "https://slank.ccnmtl.columbia.edu/accounts/caslogin/?next=/"), (True, 'test_claim', ['test_claim', u'crs-3', u'crs-1'])) mock_urlopen.assert_called_with( "https://cas.columbia.edu/cas/serviceValidate?ticket=foo" "&service=https%3A//slank.ccnmtl.columbia.edu/accounts/" "caslogin/%3Fnext%3D/")
def test_validate_ticket_success(self, mock_urlopen): self.response.read.return_value = ( "\n\n\n<cas:serviceResponse xmlns:cas='http://www." "yale.edu/tp/cas'>\n\t<cas:authenticationSuccess>\n" "\t\t<cas:user>anp8</cas:user>\n" "\n" "\n" "\t</cas:authenticationSuccess>\n" "</cas:serviceResponse>\n") mock_urlopen.return_value = self.response self.assertEqual( validate_cas2_ticket( "foo", "https://slank.ccnmtl.columbia.edu/accounts/caslogin/?next=/"), (True, 'anp8', ['anp8'])) mock_urlopen.assert_called_with( "https://cas.columbia.edu/cas/serviceValidate?ticket=foo" "&service=https%3A//slank.ccnmtl.columbia.edu/accounts/" "caslogin/%3Fnext%3D/")
def test_validate_ticket_fail(self, mock_urlopen): self.response.read.return_value = ( "\n\n\n<cas:serviceResponse xmlns:cas='http" "://www.yale.edu/tp/cas'>\n\t<cas:authenticationFailure " "code='INVALID_SERVICE'>\n\t\tticket 'ST-181952-OK0" "qr5suLueHccqPfgIT-idmcasprod2' does not match supp" "lied service. The original service was 'https://" "slank.ccnmtl.columbia.edu/accounts/caslogin/?next=/'" "and the supplied service was 'https://slank.ccnmtl." "columbia.edu/accounts/caslogin/'.\n\t</cas:authenti" "cationFailure>\n</cas:serviceResponse>") mock_urlopen.return_value = self.response self.assertEqual( validate_cas2_ticket( "foo", "https://slank.ccnmtl.columbia.edu/accounts/caslogin/?next=/"), (False, "The ticket was already used or was invalid.", [])) mock_urlopen.assert_called_with( "https://cas.columbia.edu/cas/serviceValidate?ticket=foo" "&service=https%3A//slank.ccnmtl.columbia.edu/accounts/" "caslogin/%3Fnext%3D/")
def test_no_ticket(self, mock_urlopen): self.assertEqual( validate_cas2_ticket("", ""), (False, 'no ticketid', ''))