Symfony2博客应用程序教程:第四部分(续)-测试安全页

简介:

I just wanted to write a quick post illustrating how to use the http basic authentication mechanism to test secured pages. Since the testing framework does not support sessions at the moment, it is not possible to write tests using the form login mechanism. Because of this, we have to use http basic authentication to test our secure pages.
我只想快速写一篇文章说明如何使用HTTP基本认证机制来测试安全页面。因为测试框架目前不支持会话,因此不可以使用表单登录机制来编写测试。有鉴于此,我们不得不使用HTTP基本认证来测试我们的安全页面。

First, we must make changes to the application’s test environment. The config_test.yml file located in the app/config directory is where we put all of our test environment specific configuration. We need to override the security configuration we set up in the previous tutorial to use the http basic authentication mechanism. Open up the config_test.yml file and add the following.
首先,我们必须修改应用程序的测试环境。我们将我们测试环境的相关配置全部放入了位于app/config目录中的config_test.yml文件中。我们需要覆写在先前教程中设置的安全配置,以便使用HTTP基本认证机制。打开config_test.yml文件,并添加下列语句:

 
  1. ## Security Configuration 
  2. security: 
  3.     encoders: 
  4.         Symfony\Component\Security\Core\User\User: plaintext 
  5.  
  6.     providers: 
  7.         main: 
  8.             users: 
  9.                 john.doe: { password: admin, roles: ROLE_ADMIN } 
  10.  
  11.     firewalls: 
  12.         main: 
  13.             pattern:    /.* 
  14.             http_basic: true 
  15.             logout:     true 
  16.             security: true 
  17.             anonymous: true 

Here we have declared that we want to use http_basic authentication in the test environment firewall. We have also told symfony that we want to use a plaintext password encoder for our user. This allows us to specify the user’s password in plain text. Under the providers entry we have declared an in-memory user with a username of john.doe, a password of admin and having the role ROLE_ADMIN. We will supply these credentials in our request using server parameters.
在这里,我们在测试环境的防火墙中声明我们想使用http_basic认证。我们还告诉Symfony2我们想为我们的用户使用纯文本密码编码器。这样可以让我们用纯文本指定用户的密码。在提供器条目下,我们声明了一个用户名是john.doe的in-memory用户,密码是admin,并且拥有ROLE_ADMIN角色。我们将在我们的请求里使用服务器参数来提供这些参数。

Now open up the AdminControllerTest.php file located in the src/Company/BlogBundle/Tests/Controller folder. Here is the code for the test.
现在打开位于src/Company/BlogBundle/Tests/Controller文件夹中的AdminControllerTest.php文件,以下是测试代码。

 
  1. namespace Company\BlogBundle\Tests\Controller; 
  2.   
  3. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
  4.   
  5. class AdminControllerTest extends WebTestCase 
  6.     public function testIndex() 
  7.     { 
  8.         $client = $this->createClient(); 
  9.         $client->followRedirects(true); 
  10.   
  11.         // request the index action with invalid credentials 
  12.         $crawler = $client->request('GET''/admin/'array(), array(), 
  13.             array('PHP_AUTH_USER' => 'john.doe''PHP_AUTH_PW' => 'wrong_pass')); 
  14.   
  15.         $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
  16.   
  17.         // we should be redirected to the login page 
  18.         $this->assertTrue($crawler->filter('title:contains("Login")')->count() > 0); 
  19.   
  20.         // request the index action with valid credentials 
  21.         $crawler = $client->request('GET''/admin/'array(), array(), 
  22.             array('PHP_AUTH_USER' => 'john.doe''PHP_AUTH_PW' => 'admin')); 
  23.   
  24.         $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
  25.   
  26.         // check the title of the page matches the admin home page 
  27.         $this->assertTrue($crawler->filter('title:contains("Admin | Home")')->count() > 0); 
  28.   
  29.         // check that the logout link exists 
  30.         $this->assertTrue($crawler->filter('a:contains("Logout")')->count() > 0); 
  31.     } 

The code is fairly straightforward. You should be able to follow along with the comments and know what is going on. Two special server parameters are used to pass the user’s credentials to the application PHP_AUTH_USER and PHP_AUTH_PW.
代码非常简单。您应该能够根据注解明白是怎么回事。两个特定的服务器参数(PHP_AUTH_USERPHP_AUTH_PW)用于将用户的证书发送到应用程序。

You should now be setup to test all of your secured pages. I am still not sure what I will be posting about next. I have been out of town, so I have not had time to even think about it. I am hesitant to do a Form tutorial because of the proposed changes. I was thinking about maybe going over the container and writing a custom service. Let me know what you guys want. Until next time…
您现在应该做好测试您所有安全页面的设置。我一直不确定我下一篇文章要写什么。我不在家,所以我没有时间考虑这个。我很犹豫是改主意写一篇表单教程,还是按原计划写写容器和自定义服务?让我知道您需要什么。直到下一次...


本文转自 firehare 51CTO博客,原文链接:http://blog.51cto.com/firehare/609398,如需转载请自行联系原作者

相关文章
|
6天前
|
Web App开发 JavaScript 前端开发
《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)
【5月更文挑战第3天】本文介绍了如何在Web自动化测试中使用JavaScript执行器(JavascriptExecutor)来完成Selenium API无法处理的任务。首先,需要将WebDriver转换为JavascriptExecutor对象,然后通过executeScript方法执行JavaScript代码。示例用法包括设置JS代码字符串并调用executeScript。文章提供了两个实战场景:一是当时间插件限制输入时,用JS去除元素的readonly属性;二是处理需滚动才能显示的元素,利用JS滚动页面。还给出了一个滚动到底部的代码示例,并提供了详细步骤和解释。
30 10
|
1天前
|
Web App开发 JavaScript 前端开发
《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点-上篇(详解教程)
【5月更文挑战第7天】本文介绍了如何在Java+Selenium自动化测试中处理浏览器对不信任证书的处理方法,特别是针对IE、Chrome和Firefox浏览器。在某些情况下,访问HTTPS网站时会遇到证书不可信的警告,但可以通过编程方式跳过这些警告。
13 1
|
3天前
|
传感器 监控 安全
LabVIEW开发汽车安全带张紧测试
LabVIEW开发汽车安全带张紧测试
15 3
|
3天前
|
前端开发 Java 测试技术
《手把手教你》系列技巧篇(四十二)-java+ selenium自动化测试 - 处理iframe -下篇(详解教程)
【5月更文挑战第6天】本文介绍了如何使用Selenium处理含有iframe的网页。作者首先解释了iframe是什么,即HTML中的一个框架,用于在一个页面中嵌入另一个页面。接着,通过一个实战例子展示了在QQ邮箱登录页面中,由于输入框存在于iframe内,导致直接定位元素失败。作者提供了三种方法来处理这种情况:1)通过id或name属性切换到iframe;2)使用webElement对象切换;3)通过索引切换。最后,给出了相应的Java代码示例,并提醒读者根据iframe的实际情况选择合适的方法进行切换和元素定位。
8 0
|
4天前
|
Java 测试技术 API
Spring Boot 单元测试 0基础教程
Spring Boot 单元测试 0基础教程
8 0
|
4天前
|
Linux 测试技术 Windows
LabVIEW对NI Linux RT应用程序性能进行基准测试
LabVIEW对NI Linux RT应用程序性能进行基准测试
|
4天前
|
测试技术
LabVIEW程序测试
LabVIEW程序测试
|
4天前
|
前端开发 测试技术 Python
《手把手教你》系列技巧篇(四十一)-java+ selenium自动化测试 - 处理iframe -上篇(详解教程)
【5月更文挑战第5天】本文介绍了HTML中的`iframe`标签,它用于在网页中嵌套其他网页。`iframe`常用于加载外部内容或网站的某个部分,以实现页面美观。文章还讲述了使用Selenium自动化测试时如何处理`iframe`,通过`switchTo().frame()`方法进入`iframe`,完成相应操作,然后使用`switchTo().defaultContent()`返回主窗口。此外,文章提供了一个包含`iframe`的HTML代码示例,并给出了一个简单的自动化测试代码实战,演示了如何在`iframe`中输入文本。
14 3
|
5天前
|
JavaScript 前端开发 Java
《手把手教你》系列技巧篇(四十)-java+ selenium自动化测试-JavaScript的调用执行-下篇(详解教程)
【5月更文挑战第4天】本文介绍了如何使用JavaScriptExecutor在自动化测试中实现元素高亮显示。通过创建并执行JS代码,可以改变元素的样式,例如设置背景色和边框,以突出显示被操作的元素。文中提供了一个Java示例,展示了如何在Selenium中使用此方法,并附有代码截图和运行效果展示。该技术有助于跟踪和理解测试过程中的元素交互。
8 0
|
6天前
|
Java 测试技术 持续交付
自动化测试框架选型与实战:深入探索与应用
【5月更文挑战第8天】本文探讨了自动化测试框架的选型与实战应用,强调了其在软件质量保障中的重要性。选型原则包括考虑项目需求、技术栈、可扩展性和可维护性,以及社区支持和文档。介绍了Selenium、Appium、JUnit和Pytest等常用框架,并概述了实战应用的步骤,包括明确需求、搭建环境、编写测试用例、执行测试、分析结果、维护代码和持续集成。合理选型与实践能提升测试效率,保障项目成功。

热门文章

最新文章