22import logging
33import unittest
44
5- from slack .web .classes .objects import PlainTextObject , Option
5+ from slack .errors import SlackObjectFormationError
6+ from slack .web .classes .blocks import InputBlock , SectionBlock , DividerBlock , ActionsBlock , ContextBlock
7+ from slack .web .classes .elements import PlainTextInputElement , RadioButtonsElement , CheckboxesElement , ButtonElement , \
8+ ImageElement
9+ from slack .web .classes .objects import PlainTextObject , Option , MarkdownTextObject
610from slack .web .classes .views import View , ViewState , ViewStateValue
711
812
@@ -21,6 +25,85 @@ def verify_loaded_view_object(self, file):
2125 # Modals
2226 # --------------------------------
2327
28+ def test_valid_construction (self ):
29+ modal_view = View (
30+ type = "modal" ,
31+ callback_id = "modal-id" ,
32+ title = PlainTextObject (text = "Awesome Modal" ),
33+ submit = PlainTextObject (text = "Submit" ),
34+ close = PlainTextObject (text = "Cancel" ),
35+ blocks = [
36+ InputBlock (
37+ block_id = "b-id" ,
38+ label = PlainTextObject (text = "Input label" ),
39+ element = PlainTextInputElement (action_id = "a-id" )
40+ ),
41+ InputBlock (
42+ block_id = "cb-id" ,
43+ label = PlainTextObject (text = "Label" ),
44+ element = CheckboxesElement (
45+ action_id = "a-cb-id" ,
46+ options = [
47+ Option (text = PlainTextObject (text = "*this is plain_text text*" ), value = "v1" ),
48+ Option (text = MarkdownTextObject (text = "*this is mrkdwn text*" ), value = "v2" ),
49+ ],
50+ ),
51+ ),
52+ SectionBlock (
53+ block_id = "sb-id" ,
54+ text = MarkdownTextObject (text = "This is a mrkdwn text section block." ),
55+ fields = [
56+ PlainTextObject (text = "*this is plain_text text*" , emoji = True ),
57+ MarkdownTextObject (text = "*this is mrkdwn text*" ),
58+ PlainTextObject (text = "*this is plain_text text*" , emoji = True ),
59+ ]
60+ ),
61+ DividerBlock (),
62+ SectionBlock (
63+ block_id = "rb-id" ,
64+ text = MarkdownTextObject (text = "This is a section block with radio button accessory" ),
65+ accessory = RadioButtonsElement (
66+ initial_option = Option (
67+ text = PlainTextObject (text = "Option 1" ),
68+ value = "option 1" ,
69+ description = PlainTextObject (text = "Description for option 1" ),
70+ ),
71+ options = [
72+ Option (
73+ text = PlainTextObject (text = "Option 1" ),
74+ value = "option 1" ,
75+ description = PlainTextObject (text = "Description for option 1" ),
76+ ),
77+ Option (
78+ text = PlainTextObject (text = "Option 2" ),
79+ value = "option 2" ,
80+ description = PlainTextObject (text = "Description for option 2" ),
81+ ),
82+ ]
83+ )
84+ )
85+ ]
86+ )
87+ modal_view .validate_json ()
88+
89+ def test_invalid_type_value (self ):
90+ modal_view = View (
91+ type = "modallll" ,
92+ callback_id = "modal-id" ,
93+ title = PlainTextObject (text = "Awesome Modal" ),
94+ submit = PlainTextObject (text = "Submit" ),
95+ close = PlainTextObject (text = "Cancel" ),
96+ blocks = [
97+ InputBlock (
98+ block_id = "b-id" ,
99+ label = PlainTextObject (text = "Input label" ),
100+ element = PlainTextInputElement (action_id = "a-id" )
101+ ),
102+ ]
103+ )
104+ with self .assertRaises (SlackObjectFormationError ):
105+ modal_view .validate_json ()
106+
24107 def test_simple_state_values (self ):
25108 expected = {
26109 "values" : {
@@ -270,6 +353,89 @@ def test_load_modal_view_010(self):
270353 # Home Tabs
271354 # --------------------------------
272355
356+ def test_home_tab_construction (self ):
357+ home_tab_view = View (
358+ type = "home" ,
359+ blocks = [
360+ SectionBlock (
361+ text = MarkdownTextObject (text = "*Here's what you can do with Project Tracker:*" ),
362+ ),
363+ ActionsBlock (
364+ elements = [
365+ ButtonElement (
366+ text = PlainTextObject (text = "Create New Task" , emoji = True ),
367+ style = "primary" ,
368+ value = "create_task" ,
369+ ),
370+ ButtonElement (
371+ text = PlainTextObject (text = "Create New Project" , emoji = True ),
372+ value = "create_project" ,
373+ ),
374+ ButtonElement (
375+ text = PlainTextObject (text = "Help" , emoji = True ),
376+ value = "help" ,
377+ ),
378+ ],
379+ ),
380+ ContextBlock (
381+ elements = [
382+ ImageElement (
383+ image_url = "https://api.slack.com/img/blocks/bkb_template_images/placeholder.png" ,
384+ alt_text = "placeholder" ,
385+ ),
386+ ],
387+ ),
388+ SectionBlock (
389+ text = MarkdownTextObject (text = "*Your Configurations*" ),
390+ ),
391+ DividerBlock (),
392+ SectionBlock (
393+ text = MarkdownTextObject (
394+ text = "*#public-relations*\n <fakelink.toUrl.com|PR Strategy 2019> posts new tasks, comments, and project updates to <fakelink.toChannel.com|#public-relations>" ),
395+ accessory = ButtonElement (
396+ text = PlainTextObject (text = "Edit" , emoji = True ),
397+ value = "public-relations" ,
398+ ),
399+ )
400+ ],
401+ )
402+ home_tab_view .validate_json ()
403+
404+ def test_input_blocks_in_home_tab (self ):
405+ modal_view = View (
406+ type = "home" ,
407+ callback_id = "home-tab-id" ,
408+ blocks = [
409+ InputBlock (
410+ block_id = "b-id" ,
411+ label = PlainTextObject (text = "Input label" ),
412+ element = PlainTextInputElement (action_id = "a-id" )
413+ ),
414+ ]
415+ )
416+ with self .assertRaises (SlackObjectFormationError ):
417+ modal_view .validate_json ()
418+
419+ def test_submit_in_home_tab (self ):
420+ modal_view = View (
421+ type = "home" ,
422+ callback_id = "home-tab-id" ,
423+ submit = PlainTextObject (text = "Submit" ),
424+ blocks = [DividerBlock ()]
425+ )
426+ with self .assertRaises (SlackObjectFormationError ):
427+ modal_view .validate_json ()
428+
429+ def test_close_in_home_tab (self ):
430+ modal_view = View (
431+ type = "home" ,
432+ callback_id = "home-tab-id" ,
433+ close = PlainTextObject (text = "Cancel" ),
434+ blocks = [DividerBlock ()]
435+ )
436+ with self .assertRaises (SlackObjectFormationError ):
437+ modal_view .validate_json ()
438+
273439 def test_load_home_tab_view_001 (self ):
274440 with open ("tests/data/view_home_001.json" ) as file :
275441 self .verify_loaded_view_object (file )
0 commit comments