@@ -50,6 +50,107 @@ public void Should_Allow_Credentials_With_Token()
5050 Assert . AreEqual ( "{\" user\" :{\" email\" :\" name\" ,\" password\" :\" password\" ,\" tfa_token\" :\" token\" }}" , Encoding . Default . GetString ( loginService . ByteContent ) ) ;
5151 }
5252
53+ [ TestMethod ]
54+ public void Should_Allow_Credentials_With_MfaSecret ( )
55+ {
56+
57+ string testMfaSecret = "JBSWY3DPEHPK3PXP" ; // Base32 encoded "Hello!"
58+ var loginService = new LoginService ( serializer , credentials , null , testMfaSecret ) ;
59+ loginService . ContentBody ( ) ;
60+
61+ Assert . IsNotNull ( loginService ) ;
62+ var contentString = Encoding . Default . GetString ( loginService . ByteContent ) ;
63+
64+ Assert . IsTrue ( contentString . Contains ( "\" email\" :\" name\" " ) ) ;
65+ Assert . IsTrue ( contentString . Contains ( "\" password\" :\" password\" " ) ) ;
66+ Assert . IsTrue ( contentString . Contains ( "\" tfa_token\" :" ) ) ;
67+
68+ // Verify the tfa_token is not null or empty in the JSON
69+ Assert . IsFalse ( contentString . Contains ( "\" tfa_token\" :null" ) ) ;
70+ Assert . IsFalse ( contentString . Contains ( "\" tfa_token\" :\" \" " ) ) ;
71+ }
72+
73+ [ TestMethod ]
74+ public void Should_Generate_TOTP_Token_When_MfaSecret_Provided ( )
75+ {
76+ string testMfaSecret = "JBSWY3DPEHPK3PXP" ; // Base32 encoded "Hello!"
77+ var loginService1 = new LoginService ( serializer , credentials , null , testMfaSecret ) ;
78+ var loginService2 = new LoginService ( serializer , credentials , null , testMfaSecret ) ;
79+
80+ loginService1 . ContentBody ( ) ;
81+ loginService2 . ContentBody ( ) ;
82+
83+ var content1 = Encoding . Default . GetString ( loginService1 . ByteContent ) ;
84+ var content2 = Encoding . Default . GetString ( loginService2 . ByteContent ) ;
85+
86+ // Both should contain tfa_token
87+ Assert . IsTrue ( content1 . Contains ( "\" tfa_token\" :" ) ) ;
88+ Assert . IsTrue ( content2 . Contains ( "\" tfa_token\" :" ) ) ;
89+
90+ // Extract the tokens for comparison (tokens should be 6 digits)
91+ var token1Match = System . Text . RegularExpressions . Regex . Match ( content1 , "\" tfa_token\" :\" (\\ d{6})\" " ) ;
92+ var token2Match = System . Text . RegularExpressions . Regex . Match ( content2 , "\" tfa_token\" :\" (\\ d{6})\" " ) ;
93+
94+ Assert . IsTrue ( token1Match . Success ) ;
95+ Assert . IsTrue ( token2Match . Success ) ;
96+
97+ // Tokens should be valid 6-digit numbers
98+ Assert . AreEqual ( 6 , token1Match . Groups [ 1 ] . Value . Length ) ;
99+ Assert . AreEqual ( 6 , token2Match . Groups [ 1 ] . Value . Length ) ;
100+ }
101+
102+ [ TestMethod ]
103+ public void Should_Prefer_Explicit_Token_Over_MfaSecret ( )
104+ {
105+ string testMfaSecret = "JBSWY3DPEHPK3PXP" ;
106+ // file deepcode ignore NoHardcodedCredentials/test: random test token
107+ string explicitToken = "123456" ;
108+
109+ var loginService = new LoginService ( serializer , credentials , explicitToken , testMfaSecret ) ;
110+ loginService . ContentBody ( ) ;
111+
112+ var contentString = Encoding . Default . GetString ( loginService . ByteContent ) ;
113+
114+ // Should use the explicit token, not generate one from MFA secret
115+ Assert . IsTrue ( contentString . Contains ( "\" tfa_token\" :\" 123456\" " ) ) ;
116+ }
117+
118+ [ TestMethod ]
119+ [ ExpectedException ( typeof ( ArgumentException ) ) ]
120+ public void Should_Throw_Exception_For_Invalid_Base32_MfaSecret ( )
121+ {
122+ // Invalid Base32 secret (contains invalid characters)
123+ string invalidMfaSecret = "INVALID_BASE32_123!@#" ;
124+
125+ var loginService = new LoginService ( serializer , credentials , null , invalidMfaSecret ) ;
126+ }
127+
128+ [ TestMethod ]
129+ public void Should_Not_Generate_Token_When_MfaSecret_Is_Empty ( )
130+ {
131+ var loginService = new LoginService ( serializer , credentials , null , "" ) ;
132+ loginService . ContentBody ( ) ;
133+
134+ var contentString = Encoding . Default . GetString ( loginService . ByteContent ) ;
135+
136+ // Should not contain tfa_token when MFA secret is empty
137+ Assert . IsFalse ( contentString . Contains ( "\" tfa_token\" :" ) ) ;
138+ Assert . AreEqual ( "{\" user\" :{\" email\" :\" name\" ,\" password\" :\" password\" }}" , contentString ) ;
139+ }
140+
141+ [ TestMethod ]
142+ public void Should_Not_Generate_Token_When_MfaSecret_Is_Null ( )
143+ {
144+ var loginService = new LoginService ( serializer , credentials , null , null ) ;
145+ loginService . ContentBody ( ) ;
146+
147+ var contentString = Encoding . Default . GetString ( loginService . ByteContent ) ;
148+
149+ // Should not contain tfa_token when MFA secret is null
150+ Assert . IsFalse ( contentString . Contains ( "\" tfa_token\" :" ) ) ;
151+ Assert . AreEqual ( "{\" user\" :{\" email\" :\" name\" ,\" password\" :\" password\" }}" , contentString ) ;
152+ }
153+
53154 [ TestMethod ]
54155 public void Should_Override_Authtoken_To_ContentstackOptions_On_Success ( )
55156 {
0 commit comments