Adding gc_backend tests
[gc-dialer] / tests / gc_samples / generate_gc_samples.py
1 #!/usr/bin/env python
2
3 import sys
4 import urllib
5 import urllib2
6 import traceback
7 import warnings
8
9 import sys
10 sys.path.append("../../src")
11
12 import browser_emu
13 import gc_backend
14
15         _forwardselectURL = "http://www.grandcentral.com/mobile/settings/forwarding_select"
16         _loginURL = "https://www.grandcentral.com/mobile/account/login"
17         _setforwardURL = "http://www.grandcentral.com/mobile/settings/set_forwarding?from=settings"
18         _clicktocallURL = "http://www.grandcentral.com/mobile/calls/click_to_call?a_t=%s&destno=%s"
19         _inboxallURL = "http://www.grandcentral.com/mobile/messages/inbox?types=all"
20         _contactsURL = "http://www.grandcentral.com/mobile/contacts"
21         _contactDetailURL = "http://www.grandcentral.com/mobile/contacts/detail"
22 webpages = [
23         ("forward", gc_backend.GCDialer._forwardselectURL),
24         ("login", gc_backend.GCDialer._loginURL),
25         ("setforward", gc_backend.GCDialer._setforwardURL),
26         ("clicktocall", gc_backend.GCDialer._clicktocallURL),
27         ("recent", gc_backend.GCDialer._inboxallURL),
28         ("contacts", gc_backend.GCDialer._contactsURL),
29         ("contactdetails", gc_backend.GCDialer._contactDetailURL),
30 ]
31
32
33 browser = browser_emu.MozillaEmulator(1)
34 for name, url in webpages:
35         try:
36                 page = browser.download(url)
37         except StandardError, e:
38                 print e.message
39                 continue
40         with open("not_loggedin_%s.txt" % name, "w") as f:
41                 f.write(page)
42
43 username = sys.argv[1]
44 password = sys.argv[2]
45
46 loginPostData = urllib.urlencode({
47         'Email' : username,
48         'Passwd' : password,
49         'service': "grandcentral",
50         "ltmpl": "mobile",
51         "btmpl": "mobile",
52         "PersistentCookie": "yes",
53 })
54
55 try:
56         loginSuccessOrFailurePage = browser.download(gc_backend.GCDialer._loginURL, loginPostData)
57 except urllib2.URLError, e:
58         warnings.warn(traceback.format_exc())
59         raise RuntimeError("%s is not accesible" % gc_backend.GCDialer._loginURL)
60
61 forwardPage = browser.download(gc_backend.GCDialer._forwardURL)
62
63 tokenGroup = gc_backend.GCDialer._tokenRe.search(forwardPage)
64 if tokenGroup is None:
65         print forwardPage
66         raise RuntimeError("Could not extract authentication token from GoogleVoice")
67 token = tokenGroup.group(1)
68
69 browser = browser_emu.MozillaEmulator(1)
70 for name, url in webpages:
71         try:
72                 #data = urllib.urlencode({
73                 #       "_rnr_se": token,
74                 #})
75                 #page = browser.download(url, data)
76                 page = browser.download(url)
77         except StandardError, e:
78                 warnings.warn(traceback.format_exc())
79                 continue
80         print "Writing to file"
81         with open("loggedin_%s.txt" % name, "w") as f:
82                 f.write(page)