def test_init_with_default_strict_errors_on_unassigned(self): with self.assertRaises(ValueError): structs.JID("\U0001f601", "example.com", "bar") with self.assertRaises(ValueError): structs.JID("foo", "\U0001f601example.com", "bar") with self.assertRaises(ValueError): structs.JID("foo", "example.com", "\U0001f601")
def test_reject_long_localpart(self): with self.assertRaisesRegex(ValueError, "too long"): structs.JID("x"*1024, "foo", None) with self.assertRaisesRegex(ValueError, "too long"): structs.JID("ü"*512, "foo", None) with self.assertRaisesRegex(ValueError, "too long"): structs.JID.fromstr("ü"*512 + "@foo")
def test_fromstr_full(self): self.assertEqual(structs.JID("foo", "example.test", "bar"), structs.JID.fromstr("[email protected]/bar")) self.assertEqual(structs.JID("ßA", "IX.test", "\u2168"), structs.JID.fromstr("[email protected]/IX")) self.assertEqual(structs.JID("ßA", "IX.test", "bar@baz/fnord"), structs.JID.fromstr("[email protected]/bar@baz/fnord"))
def test_reject_long_domainpart(self): with self.assertRaisesRegex(ValueError, "too long"): structs.JID(None, "x"*1024, None) with self.assertRaisesRegex(ValueError, "too long"): structs.JID(None, "ü"*512, None) with self.assertRaisesRegex(ValueError, "too long"): structs.JID.fromstr("ü"*512)
def test_reject_long_resource(self): with self.assertRaisesRegex(ValueError, "too long"): structs.JID(None, "foo", "x"*1024) with self.assertRaisesRegex(ValueError, "too long"): structs.JID(None, "foo", "ü"*512) with self.assertRaisesRegex(ValueError, "too long"): structs.JID.fromstr("foo/" + "ü"*512)
def test_fromstr_bare(self): self.assertEqual( structs.JID("foo", "example.test", None), structs.JID.fromstr("*****@*****.**") ) self.assertEqual( structs.JID("ßA", "IX.test", None), structs.JID.fromstr("*****@*****.**") )
def test_fromstr_domain(self): self.assertEqual( structs.JID(None, "example.test", None), structs.JID.fromstr("example.test") ) self.assertEqual( structs.JID(None, "IX.test", None), structs.JID.fromstr("ix.test") )
def test_init_domain_jid(self): j = structs.JID(None, "example.test", None) self.assertIsNone(j.resource) self.assertIsNone(j.localpart) self.assertEqual( "example.test", j.domain)
def test_fromstr_domain_nonstrict(self): self.assertEqual( structs.JID("\U0001f601", "\U0001f601example.test", "\U0001f601", strict=False), structs.JID.fromstr("\U0001f601@\U0001f601example.test/\U0001f601", strict=False) )
def test_replace(self): j = structs.JID("foo", "example.com", "bar") j2 = j.replace(localpart="fnord", domain="example.invalid", resource="baz") self.assertEqual("fnord", j2.localpart) self.assertEqual("example.invalid", j2.domain) self.assertEqual("baz", j2.resource)
def test_replace_ignores_problems_on_existing_parts(self): j = structs.JID( "\U0001f601foo", "\U0001f601example.test", "\U0001f601bar", strict=False, ) j2 = j.replace() self.assertEqual(j, j2)
def test_replace_checks_replaced_strings(self): j = structs.JID( "\U0001f601foo", "\U0001f601example.test", "\U0001f601bar", strict=False, ) with self.assertRaises(ValueError): j.replace( domain=j.domain )
def test_init_full(self): j = structs.JID("foo", "example.com", "bar") self.assertEqual( "foo", j.localpart) self.assertEqual( "example.com", j.domain) self.assertEqual( "bar", j.resource)
def test_replace_nonstrict_allows_unassigned_codepoints(self): j = structs.JID( "\U0001f601foo", "\U0001f601example.test", "\U0001f601bar", strict=False, ) j2 = j.replace( domain=j.domain, strict=False, ) self.assertEqual(j, j2)
def test_replace_enforces_stringprep(self): j = structs.JID("foo", "example.com", "bar") with self.assertRaises(ValueError): j.replace(localpart="\u0007") with self.assertRaises(ValueError): j.replace(domain="\u070f") with self.assertRaises(ValueError): j.replace(resource="\u0007") self.assertEqual("ssa", j.replace(localpart="ßA").localpart) self.assertEqual("ix.test", j.replace(domain="IX.test").domain) self.assertEqual("IX", j.replace(resource="\u2168").resource)
def test_init_enforces_stringprep(self): with self.assertRaises(ValueError): structs.JID("\u0007", "example.com", "bar") with self.assertRaises(ValueError): structs.JID("foo", "\u070f", "bar") with self.assertRaises(ValueError): structs.JID("foo", "example.com", "\u0007") self.assertEqual("ssa", structs.JID("ßA", "example.test", None).localpart) self.assertEqual("ix.test", structs.JID(None, "IX.test", None).domain) self.assertEqual("IX", structs.JID(None, "example.test", "\u2168").resource)
def test_reject_empty_resource(self): with self.assertRaises(ValueError): structs.JID("foo", "bar.baz", "") with self.assertRaises(ValueError): structs.JID.fromstr("[email protected]/")
def test_reject_empty_domainpart(self): with self.assertRaises(ValueError): structs.JID("foo", "", None) with self.assertRaises(ValueError): structs.JID.fromstr("foo@")
def test_reject_empty_localpart(self): with self.assertRaises(ValueError): structs.JID("", "bar.baz", None) with self.assertRaises(ValueError): structs.JID.fromstr("@bar.baz")
def test_str_full_jid(self): j = structs.JID("foo", "example.test", "bar") self.assertEqual( "[email protected]/bar", str(j))
def test_fromstr_domain_with_funny_resource(self): self.assertEqual( structs.JID(None, "example.test", "foo@bar"), structs.JID.fromstr("example.test/foo@bar") )
def test_replace_rejects_surplus_argument(self): j = structs.JID("foo", "example.test", "bar") with self.assertRaises(TypeError): j.replace(foobar="baz")
def test_str_domain_jid(self): j = structs.JID(None, "example.test", None) self.assertEqual( "example.test", str(j))
def test_is_domain(self): self.assertFalse(structs.JID("foo", "example.test", "bar").is_domain) self.assertFalse(structs.JID("foo", "example.test", None).is_domain) self.assertTrue(structs.JID(None, "example.test", None).is_domain)
def test_bare(self): j = structs.JID("foo", "example.test", "bar") self.assertEqual( structs.JID("foo", "example.test", None), j.bare())
def test_immutable(self): j = structs.JID(None, "example.test", None) with self.assertRaises(AttributeError): j.foo = "bar"
def test_replace_domain_jid(self): j = structs.JID("foo", "example.test", "bar") self.assertEqual( structs.JID(None, "example.test", None), j.replace(localpart=None, resource=None) )
def test_replace_require_domainpart(self): j = structs.JID("foo", "example.test", "bar") with self.assertRaises(ValueError): j.replace(domain=None)
def test_str_bare_jid(self): j = structs.JID("foo", "example.test", None) self.assertEqual( "*****@*****.**", str(j))
def test_require_domainpart(self): with self.assertRaises(ValueError): structs.JID(None, None, None)