1+ # Copyright The IETF Trust 2021, All Rights Reserved
2+ # -*- coding: utf-8 -*-
3+
4+ import debug # pyflakes:ignore
5+
6+ from ietf .doc .factories import WgDraftFactory , DocumentAuthorFactory
7+ from ietf .person .factories import PersonFactory
8+ from ietf .person .models import Person
9+ from ietf .utils .jstest import IetfSeleniumTestCase , ifSeleniumEnabled , selenium_enabled
10+
11+ if selenium_enabled ():
12+ from selenium .webdriver .common .by import By
13+ from selenium .webdriver .support .ui import WebDriverWait
14+ from selenium .webdriver .support import expected_conditions
15+
16+
17+ class presence_of_element_child_by_css_selector :
18+ """Wait for presence of a child of a WebElement matching a CSS selector
19+
20+ This is a condition class for use with WebDriverWait.
21+ """
22+ def __init__ (self , element , child_selector ):
23+ self .element = element
24+ self .child_selector = child_selector
25+
26+ def __call__ (self , driver ):
27+ child = self .element .find_element_by_css_selector (self .child_selector )
28+ return child if child is not None else False
29+
30+ @ifSeleniumEnabled
31+ class EditAuthorsTests (IetfSeleniumTestCase ):
32+ def setUp (self ):
33+ super (EditAuthorsTests , self ).setUp ()
34+ self .wait = WebDriverWait (self .driver , 2 )
35+
36+ def test_add_author_forms (self ):
37+ def _fill_in_author_form (form_elt , name , email , affiliation , country ):
38+ """Fill in an author form on the edit authors page
39+
40+ The form_elt input should be an element containing all the relevant inputs.
41+ """
42+ # To enter the person, type their name in the select2 search box, wait for the
43+ # search to offer the result, then press 'enter' to accept the result and close
44+ # the search input.
45+ person_span = form_elt .find_element_by_class_name ('select2-chosen' )
46+ self .scroll_to_element (person_span )
47+ person_span .click ()
48+ input = self .driver .switch_to .active_element
49+ input .send_keys (name )
50+ result_selector = 'ul.select2-results > li > div.select2-result-label'
51+ self .wait .until (
52+ expected_conditions .text_to_be_present_in_element (
53+ (By .CSS_SELECTOR , result_selector ),
54+ name
55+ ))
56+ input .send_keys ('\n ' ) # select the object
57+
58+ # After the author is selected, the email select options will be populated.
59+ # Wait for that, then click on the option corresponding to the requested email.
60+ # This will only work if the email matches an address for the selected person.
61+ email_select = form_elt .find_element_by_css_selector ('select[name$="email"]' )
62+ email_option = self .wait .until (
63+ presence_of_element_child_by_css_selector (email_select , 'option[value="{}"]' .format (email ))
64+ )
65+ email_option .click () # select the email
66+
67+ # Fill in the affiliation and country. Finally, simple text inputs!
68+ affil_input = form_elt .find_element_by_css_selector ('input[name$="affiliation"]' )
69+ affil_input .send_keys (affiliation )
70+ country_input = form_elt .find_element_by_css_selector ('input[name$="country"]' )
71+ country_input .send_keys (country )
72+
73+ def _read_author_form (form_elt ):
74+ """Read values from an author form
75+
76+ Note: returns the Person instance named in the person field, not just their name.
77+ """
78+ hidden_person_input = form_elt .find_element_by_css_selector ('input[type="text"][name$="person"]' )
79+ email_select = form_elt .find_element_by_css_selector ('select[name$="email"]' )
80+ affil_input = form_elt .find_element_by_css_selector ('input[name$="affiliation"]' )
81+ country_input = form_elt .find_element_by_css_selector ('input[name$="country"]' )
82+ return (
83+ Person .objects .get (pk = hidden_person_input .get_attribute ('value' )),
84+ email_select .get_attribute ('value' ),
85+ affil_input .get_attribute ('value' ),
86+ country_input .get_attribute ('value' ),
87+ )
88+
89+ # Create testing resources
90+ draft = WgDraftFactory ()
91+ DocumentAuthorFactory (document = draft )
92+ authors = PersonFactory .create_batch (2 ) # authors we will add
93+ orgs = ['some org' , 'some other org' ] # affiliations for the authors
94+ countries = ['France' , 'Uganda' ] # countries for the authors
95+ url = self .absreverse ('ietf.doc.views_doc.edit_authors' , kwargs = dict (name = draft .name ))
96+
97+ # Star the test by logging in with appropriate permissions and retrieving the edit page
98+ self .login ('secretary' )
99+ self .driver .get (url )
100+
101+ # The draft has one author to start with. Find the list and check the count.
102+ authors_list = self .driver .find_element_by_id ('authors-list' )
103+ author_forms = authors_list .find_elements_by_class_name ('author-panel' )
104+ self .assertEqual (len (author_forms ), 1 )
105+
106+ # get the "add author" button so we can add blank author forms
107+ add_author_button = self .driver .find_element_by_id ('add-author-button' )
108+ for index , auth in enumerate (authors ):
109+ self .scroll_to_element (add_author_button ) # Can only click if it's in view!
110+ add_author_button .click () # Create a new form. Automatically scrolls to it.
111+ author_forms = authors_list .find_elements_by_class_name ('author-panel' )
112+ authors_added = index + 1
113+ self .assertEqual (len (author_forms ), authors_added + 1 ) # Started with 1 author, hence +1
114+ _fill_in_author_form (author_forms [index + 1 ], auth .name , str (auth .email ()), orgs [index ], countries [index ])
115+
116+ # Check that the author forms have correct (and distinct) values
117+ first_auth = draft .documentauthor_set .first ()
118+ self .assertEqual (
119+ _read_author_form (author_forms [0 ]),
120+ (first_auth .person , str (first_auth .email ), first_auth .affiliation , first_auth .country ),
121+ )
122+ for index , auth in enumerate (authors ):
123+ self .assertEqual (
124+ _read_author_form (author_forms [index + 1 ]),
125+ (auth , str (auth .email ()), orgs [index ], countries [index ]),
126+ )
127+
128+ # Must provide a "basis" (change reason)
129+ self .driver .find_element_by_id ('id_basis' ).send_keys ('change testing' )
130+ # Now click the 'submit' button and check that the update was accepted.
131+ submit_button = self .driver .find_element_by_css_selector ('button[type="submit"]' )
132+ self .scroll_to_element (submit_button )
133+ submit_button .click ()
134+ # Wait for redirect to the document_main view
135+ self .wait .until (
136+ expected_conditions .url_to_be (
137+ self .absreverse ('ietf.doc.views_doc.document_main' , kwargs = dict (name = draft .name ))
138+ ))
139+ # Just a basic check that the expected authors show up. Details of the updates
140+ # are tested separately.
141+ self .assertEqual (
142+ list (draft .documentauthor_set .values_list ('person' , flat = True )),
143+ [first_auth .person .pk ] + [auth .pk for auth in authors ]
144+ )
0 commit comments