Ticket #4930: 4930-rnto-3.diff

File 4930-rnto-3.diff, 3.7 KB (added by adiroiban, 4 years ago)
  • twisted/test/test_ftp.py

     
    8181        self.dirPath = filepath.FilePath(self.directory)
    8282
    8383        # Start the server
    84         p = portal.Portal(ftp.FTPRealm(self.directory))
     84        p = portal.Portal(ftp.FTPRealm(
     85            anonymousRoot=self.directory,
     86            userHome=self.directory,
     87            ))
    8588        p.registerChecker(checkers.AllowAnonymousAccess(),
    8689                          credentials.IAnonymous)
     90
     91        users_checker = checkers.InMemoryUsernamePasswordDatabaseDontUse()
     92        self.username = "test-user"
     93        self.password = "test-password"
     94        users_checker.addUser(self.username, self.password)
     95        p.registerChecker(users_checker, credentials.IUsernamePassword)
     96
    8797        self.factory = ftp.FTPFactory(portal=p,
    8898                                      userAnonymous=self.userAnonymous)
    8999        port = reactor.listenTCP(0, self.factory, interface="127.0.0.1")
     
    159169            ['230 Anonymous login ok, access restrictions apply.'],
    160170            chainDeferred=d)
    161171
     172    def _userLogin(self):
     173        """Authenticates the FTP client using the test account."""
     174        d = self.assertCommandResponse(
     175            'USER %s' % (self.username),
     176            ['331 Password required for %s.' % (self.username)])
     177        return self.assertCommandResponse(
     178            'PASS %s' % (self.password),
     179            ['230 User logged in, proceed'],
     180            chainDeferred=d)
    162181
    163182
    164183class FTPAnonymousTestCase(FTPServerTestCase):
     
    342361        self.serverProtocol.transport.loseConnection()
    343362    testPASV = defer.deferredGenerator(testPASV)
    344363
    345     def testSYST(self):
     364    def test_SYST(self):
     365        """SYST command will always return UNIX Type: L8"""
    346366        d = self._anonymousLogin()
    347367        self.assertCommandResponse('SYST', ["215 UNIX Type: L8"],
    348368                                   chainDeferred=d)
    349369        return d
    350370
     371    def test_RNFRandRNTO(self):
     372        """
     373        Sending the RNFR command followed by RNTO should perform a
     374        successful rename operation.
     375        """
     376        # Create user home folder with a 'foo' file.
     377        self.dirPath.child(self.username).createDirectory()
     378        self.dirPath.child(self.username).child('foo').touch()
    351379
     380        d = self._userLogin()
     381        self.assertCommandResponse(
     382            'RNFR foo',
     383            ["350 Requested file action pending further information."],
     384            chainDeferred=d)
     385        self.assertCommandResponse(
     386            'RNTO bar',
     387            ["250 Requested File Action Completed OK"],
     388            chainDeferred=d)
     389
     390        def check_rename(result):
     391            self.assertTrue(
     392                self.dirPath.child(self.username).child('bar').exists())
     393            return result
     394
     395        d.addCallback(check_rename)
     396        return d
     397
     398    def test_RNFRwithoutRNTO(self):
     399        """
     400        Sending the RNFR command followed by any command other than RNTO
     401        should return an error informing users that RNFR should be followed
     402        by RNTO.
     403        """
     404        d = self._anonymousLogin()
     405        self.assertCommandResponse(
     406            'RNFR foo',
     407            ["350 Requested file action pending further information."],
     408            chainDeferred=d)
     409        self.assertCommandFailed(
     410            'OTHER don-tcare',
     411            ["503 Incorrect sequence of commands: RNTO required after RNFR"],
     412            chainDeferred=d)
     413        return d
     414
    352415    def test_portRangeForwardError(self):
    353416        """
    354417        Exceptions other than L{error.CannotListenError} which are raised by