source: .tests/test1.py @ 5a1b05c

Last change on this file since 5a1b05c was 5a1b05c, checked in by / <>, at 2021-02-25T17:52:54Z

maybe py syntax checking tools exist

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[90417ce]1import socket, sys, time, select
2
3class ircClient:
4    def __init__(self, nick, pw):
5        self.nick = nick
6        self.pw = pw
7        self.log = ''
8        self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9
[ef2cbca]10    def sendRaw(self, msg, loud = True):
[d24e6f7]11        self.receive()
[ef2cbca]12        if loud:
13            print('FROM '+ self.nick + '|| ' + msg)
[90417ce]14        self.log += msg+'\r\n'
15        self.sck.send((msg+'\r\n').encode())
16
[ef2cbca]17    def sendPrivMsg(self, recip, msg, loud = True):
[f6778cf]18        self.sendRaw('PRIVMSG '+recip+' :'+msg, loud)
[90417ce]19
20    def connect(self):
[314c3f8]21        try:
22            self.sck.connect(('127.0.0.1', 6667))
23        except:
[ef2cbca]24            print("IRC connection failed for " + self.nick)
[314c3f8]25            sys.exit(1)
[ef2cbca]26       
27        print("IRC connection established for " + self.nick)
[314c3f8]28
[90417ce]29        self.sendRaw('USER ' + (self.nick + " ")*3)
30        self.sendRaw('NICK ' + self.nick)
31        self.sendRaw('JOIN &bitlbee')
32
[314c3f8]33    def jabberLogin(self):
[90417ce]34        self.sendPrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw)
35        time.sleep(0.3)
36        self.sendPrivMsg("&bitlbee", "account on")
[314c3f8]37        time.sleep(1)
38        self.receive()
39        if self.log.find('Logged in') == -1:
[ef2cbca]40            print("Jabber login failed for " + self.nick)
[314c3f8]41            sys.exit(1)
[ef2cbca]42        else:
43            print("Jabber login successful for " + self.nick)
[90417ce]44
45    def receive(self):
46        text = ''
47        while True:
48            readable, _, _ = select.select([self.sck], [], [], 5)
49            if self.sck in readable:
50                text += self.sck.recv(2040).decode()
51                for line in text.split('\n'):
52                    if line.find('PING') != -1:
53                        self.sendRaw('PONG ' + line.split()[1])
54            else:
55                break
56        self.log += text
57        return text
58
[43a257d]59    def addJabberBuddy(self, nick):
60        self.sendPrivMsg("&bitlbee", "add 0 " + nick+"@localhost")
[ef2cbca]61   
62    def blockJabberBuddy(self, nick):
63        self.sendPrivMsg("&bitlbee", "block " + nick)
[43a257d]64
[ef2cbca]65    def unblockJabberBuddy(self, nick):
66        self.sendPrivMsg("&bitlbee", "allow " + nick)
67
[56c9fbf]68    def renameJabberBuddy(self, oldnick, newnick):
[ef2cbca]69        self.sendPrivMsg("&bitlbee", "rename " + oldnick + " " + newnick)
70       
71def testSendMessage(sender, receiver, message, shouldreceive = True):
72    sender.sendPrivMsg(b.nick, message)
[12886d4]73    received = receiver.receive().find(message) != -1
[ef2cbca]74    if shouldreceive ^ received:
75        print('Test failed: Message from ' + sender.nick + ' to ' + receiver.nick)
76        print('Sender Log:' + a.log)
77        print('Receiver Log:' + b.log)
78        sys.exit(1)
[43a257d]79
[90417ce]80def runTests():
81    clis = []
82    clis += [ircClient('test1', 'asd')]
83    clis += [ircClient('test2', 'asd')]
84    for cli in clis:
85        cli.connect()
[314c3f8]86        cli.jabberLogin()
[d24e6f7]87
[ef2cbca]88    clis[0].addJabberBuddy(clis[1].nick)
[43a257d]89
[ef2cbca]90    print("Test: Send message")
91    testSendMessage(clis[0],clis[1]),'ohai <3')
92    testSendMessage(clis[1],clis[0]),'uwu *pounces*')
93    print("Test passed")
94
95    print("Test: Block/Unblock")
96    clis[0].blockJabberBuddy(clis[1].nick)
97    testSendMessage(clis[1],clis[0]),'m-meow?', shouldreceive = False)
98    clis[0].unblockJabberBuddy(clis[1].nick)
99    testSendMessage(clis[1],clis[0]),'*purrs*')
100    print("Test passed")
101   
[90417ce]102if __name__ == "__main__":
103    runTests()
Note: See TracBrowser for help on using the repository browser.