Module:Sensitive IP addresses/blocktext/testcases explained

-- Unit tests for

local mBlockText = require('Module:Sensitive IP addresses/blocktext')local ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new

local mBlockTextFunctions = mBlockText._exportFunctionslocal parseTitle = mBlockTextFunctions.parseTitlelocal fetchSensitivityData = mBlockTextFunctions.fetchSensitivityDatalocal parseSensitivityData = mBlockTextFunctions.parseSensitivityDatalocal renderMessage = mBlockTextFunctions.renderMessage

--------------------------------------------------------------------------------- parseTitle tests-------------------------------------------------------------------------------

function suite:test_parse_special_block_with_ipv4_address self:assertEquals('1.2.3.4', parseTitle(mw.title.new('Special:Block/1.2.3.4')) )end

function suite:test_parse_special_block_with_ipv4_range self:assertEquals('1.2.3.0/24', parseTitle(mw.title.new('Special:Block/1.2.3.0/24')) )end

function suite:test_parse_special_block_with_ipv6_address self:assertEquals('2001:db8::ff00:12:3456', parseTitle(mw.title.new('Special:Block/2001:db8::ff00:12:3456')) )end

function suite:test_parse_special_block_with_ipv6_range self:assertEquals('2001:db8::ff00:12:0/122', parseTitle(mw.title.new('Special:Block/2001:db8::ff00:12:0/122')) )end

function suite:test_parse_special_block_with_username self:assertEquals(nil, parseTitle(mw.title.new('Special:Block/Example')) )end

function suite:test_parse_special_block_with_non_normalized_ipv6_address self:assertEquals('2001:db8::ff00:12:3456', parseTitle(mw.title.new('Special:Block/2001:db8:0:0::ff00:12:3456')) )end

function suite:test_parse_special_block_with_invalid_ipv4_address self:assertEquals(nil, parseTitle(mw.title.new('Special:Block/1.2.3.256')) )end

function suite:test_parse_special_block_in_french self:assertEquals('1.2.3.4', parseTitle(mw.title.new('Spécial:Bloquer/1.2.3.4')) )end

--------------------------------------------------------------------------------- fetchSensitivityData tests---- Here we test against the live sensitivity data, which means that changes to-- that module could make these tests fail. This is bad for isolation but a-- good quick check that the interface is working. To reduce the chance of test-- failures because of data changes, only test one or two items, only test the-- items that are least likely to change, and only test the data points that we-- need.-------------------------------------------------------------------------------

function suite:test_fetch_sensitivity_data_for_us_senate_ip local ipData = fetchSensitivityData('156.33.0.1') self:assertEquals('ussenate', ipData.sensitiveips.matches[1]['entity-id']) self:assertEquals('156.33.0.1', ipData.sensitiveips.matches[1].ip) self:assertTrue(ipData.sensitiveips.entities.ussenate.description:find('Senate')) self:assertEquals('political', ipData.sensitiveips.entities.ussenate.reason)end

function suite:test_fetch_sensitivity_data_for_us_senate_range local ipData = fetchSensitivityData('156.33.0.0/24') self:assertEquals('ussenate', ipData.sensitiveips.matches[1]['entity-id']) self:assertEquals('156.33.0.0/24', ipData.sensitiveips.matches[1].range) self:assertTrue(ipData.sensitiveips.entities.ussenate.description:find('Senate')) self:assertEquals('political', ipData.sensitiveips.entities.ussenate.reason)end

function suite:test_fetch_sensitivity_data_for_wmf_ip local ipData = fetchSensitivityData('185.15.56.1') self:assertEquals('wmf', ipData.sensitiveips.matches[1]['entity-id']) self:assertEquals('185.15.56.1', ipData.sensitiveips.matches[1].ip) self:assertTrue(ipData.sensitiveips.entities.wmf.description:find('Wikimedia Foundation')) self:assertEquals('technical', ipData.sensitiveips.entities.wmf.reason)end

function suite:test_fetch_sensitivity_data_for_private_ip local ipData = fetchSensitivityData('10.0.0.1') self:assertEquals(0, #ipData.sensitiveips.matches)end

--------------------------------------------------------------------------------- parseSensitivityData tests-------------------------------------------------------------------------------

function suite:test_parse_sensitivity_data_for_us_senate_ip local ipData = self:assertDeepEquals(parseSensitivityData(ipData) )end

function suite:test_parse_sensitivity_data_for_us_senate_range local ipData = self:assertDeepEquals(parseSensitivityData(ipData) )end

function suite:test_parse_sensitivity_data_when_no_match_occurs local ipData = self:assertEquals(nil, parseSensitivityData(ipData))end

function suite:test_parse_sensitivity_data_when_an_error_occurs local ipData = self:assertEquals(nil, parseSensitivityData(ipData))end

--------------------------------------------------------------------------------- renderMessage tests-------------------------------------------------------------------------------

function suite:test_render_message_template_with_ip self:assertEquals('1: ip, 2: 156.33.0.1, 3: 156.33.0.0/16, 4: ussenate, ' .. '5: United States Senate, 6: the United States Senate, ' .. '7: political', renderMessage('1: $1, 2: $2, 3: $3, 4: $4, 5: $5, 6: $6, 7: $7', ) )end

function suite:test_render_message_template_with_range self:assertEquals('1: range, 2: 156.33.0.0/24, 3: 156.33.0.0/16, 4: ussenate, ' .. '5: United States Senate, 6: the United States Senate, ' .. '7: political', renderMessage('1: $1, 2: $2, 3: $3, 4: $4, 5: $5, 6: $6, 7: $7', ) )end

function suite:test_render_message_template_with_ip_wikitext self:assertEquals('IP address', renderMessage('', ) )end

---------------------------------------------------------------------------------- Main tests--------------------------------------------------------------------------------

function suite:test_main self:assertEquals('The IP address 156.33.0.1 belongs to the entity with id "ussenate".', mBlockText._main(mw.title.new('Special:Block/156.33.0.1') ) )endreturn suite