@@ -1956,6 +1956,202 @@ private async Task QueryForAdminTest<T>(bool isExceptionExpected) where T : Cont
19561956 Assert . AreEqual ( "Item1, Item2, Item3, Item4" , typeNames ) ;
19571957 }
19581958
1959+ /* ============================================================================ AUTHENTICATION */
1960+
1961+ [ TestMethod ]
1962+ public async Task Repository_Auth_GetCurrentUser_ValidUser_ValidToken ( )
1963+ {
1964+ // ALIGN
1965+ var restCaller = CreateRestCallerFor ( @"{ ""d"": {
1966+ ""Name"": ""Admin"", ""Id"": 1, ""Type"": ""User"" }}" ) ;
1967+ var repositories = GetRepositoryCollection ( services =>
1968+ {
1969+ services . AddSingleton ( restCaller ) ;
1970+ } ) ;
1971+ var repository = await repositories . GetRepositoryAsync ( "local" , CancellationToken . None )
1972+ . ConfigureAwait ( false ) ;
1973+
1974+ // this is a test token containing the admin id (1) as a SUB
1975+ repository . Server . Authentication . AccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwibm" +
1976+ "FtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.ZU43TYZENiuL" +
1977+ "dKJPpd-hnkFhRkpLPurixsKr-8m-kBc" ;
1978+
1979+ // ACT
1980+ var content = await repository . GetCurrentUserAsync ( CancellationToken . None )
1981+ . ConfigureAwait ( false ) ;
1982+
1983+ // ASSERT
1984+ var requestedUri = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 1 ] . GetArguments ( ) . First ( ) ! ;
1985+ Assert . IsNotNull ( requestedUri ) ;
1986+ Assert . AreEqual ( "/OData.svc/content(1)?metadata=no" , requestedUri . PathAndQuery ) ;
1987+
1988+ Assert . IsNotNull ( content ) ;
1989+ Assert . AreEqual ( "Admin" , content . Name ) ;
1990+ }
1991+ [ TestMethod ]
1992+ public async Task Repository_Auth_GetCurrentUser_ValidUser_ValidToken_WithParameters ( )
1993+ {
1994+ // ALIGN
1995+ var restCaller = CreateRestCallerFor ( @"{ ""d"": {
1996+ ""Name"": ""Admin"", ""Id"": 1, ""Type"": ""User"" }}" ) ;
1997+ var repositories = GetRepositoryCollection ( services =>
1998+ {
1999+ services . AddSingleton ( restCaller ) ;
2000+ } ) ;
2001+ var repository = await repositories . GetRepositoryAsync ( "local" , CancellationToken . None )
2002+ . ConfigureAwait ( false ) ;
2003+
2004+ // this is a test token containing the admin id (1) as a SUB
2005+ repository . Server . Authentication . AccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwibm" +
2006+ "FtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.ZU43TYZENiuL" +
2007+ "dKJPpd-hnkFhRkpLPurixsKr-8m-kBc" ;
2008+
2009+ // ACT
2010+ // define select and expand parameters
2011+ var content = await repository . GetCurrentUserAsync (
2012+ new [ ] { "Id" , "Name" , "Type" , "Manager/Name" } ,
2013+ new [ ] { "Manager" } ,
2014+ CancellationToken . None ) . ConfigureAwait ( false ) ;
2015+
2016+ // ASSERT
2017+ var requestedUri = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 1 ] . GetArguments ( ) . First ( ) ! ;
2018+ Assert . IsNotNull ( requestedUri ) ;
2019+ Assert . AreEqual ( "/OData.svc/content(1)?metadata=no&$expand=Manager&$select=Id,Name,Type,Manager/Name" ,
2020+ requestedUri . PathAndQuery ) ;
2021+
2022+ Assert . IsNotNull ( content ) ;
2023+ Assert . AreEqual ( "Admin" , content . Name ) ;
2024+ }
2025+ [ TestMethod ]
2026+ public async Task Repository_Auth_GetCurrentUser_ValidUser_ExpiredToken ( )
2027+ {
2028+ // ALIGN
2029+ var restCaller = Substitute . For < IRestCaller > ( ) ;
2030+
2031+ // first call: expired token, inaccessible user id
2032+ restCaller
2033+ . GetResponseStringAsync ( Arg . Is < Uri > ( uri => uri . PathAndQuery . Contains ( "/OData.svc/content(123456)" ) ) ,
2034+ Arg . Any < HttpMethod > ( ) , Arg . Any < string > ( ) ,
2035+ Arg . Any < Dictionary < string , IEnumerable < string > > > ( ) ,
2036+ Arg . Any < CancellationToken > ( ) )
2037+ . Returns ( Task . FromResult ( string . Empty ) ) ;
2038+
2039+ // second call: GetCurrentUser action, returns the Visitor user
2040+ restCaller . GetResponseStringAsync ( Arg . Is < Uri > ( uri => uri . PathAndQuery . Contains ( "/OData.svc/('Root')/GetCurrentUser" ) ) ,
2041+ Arg . Any < HttpMethod > ( ) , Arg . Any < string > ( ) ,
2042+ Arg . Any < Dictionary < string , IEnumerable < string > > > ( ) ,
2043+ Arg . Any < CancellationToken > ( ) )
2044+ . Returns ( Task . FromResult ( @"{ ""Name"": ""Visitor"", ""Id"": 6, ""Type"": ""User"" }" ) ) ;
2045+
2046+ var repositories = GetRepositoryCollection ( services =>
2047+ {
2048+ services . AddSingleton ( restCaller ) ;
2049+ } ) ;
2050+ var repository = await repositories . GetRepositoryAsync ( "local" , CancellationToken . None )
2051+ . ConfigureAwait ( false ) ;
2052+
2053+ // this is a test token containing 123456 (INACCESSIBLE user) as a SUB
2054+ repository . Server . Authentication . AccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMj" +
2055+ "M0NTYiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE1MTYyMzkwM" +
2056+ "jJ9.MkiS50WhvOFwrwxQzd5Kp3VzkQUZhvex3kQv-CLeS3M" ;
2057+
2058+ // ACT
2059+ var content = await repository . GetCurrentUserAsync ( CancellationToken . None )
2060+ . ConfigureAwait ( false ) ;
2061+
2062+ // ASSERT
2063+ var requestedUri1 = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 1 ] . GetArguments ( ) . First ( ) ! ;
2064+ Assert . AreEqual ( "/OData.svc/content(123456)?metadata=no" , requestedUri1 . PathAndQuery ) ;
2065+
2066+ var requestedUri2 = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 2 ] . GetArguments ( ) . First ( ) ! ;
2067+ Assert . AreEqual ( "/OData.svc/('Root')/GetCurrentUser?metadata=no" , requestedUri2 . PathAndQuery ) ;
2068+
2069+ Assert . IsNotNull ( content ) ;
2070+ Assert . AreEqual ( "Visitor" , content . Name ) ;
2071+ }
2072+ [ TestMethod ]
2073+ public async Task Repository_Auth_GetCurrentUser_ValidUser_UnknownToken ( )
2074+ {
2075+ // ALIGN
2076+ var restCaller = CreateRestCallerFor ( @"{""Name"": ""Admin"", ""Id"": 1, ""Type"": ""User"" }" ) ;
2077+ var repositories = GetRepositoryCollection ( services =>
2078+ {
2079+ services . AddSingleton ( restCaller ) ;
2080+ } ) ;
2081+ var repository = await repositories . GetRepositoryAsync ( "local" , CancellationToken . None )
2082+ . ConfigureAwait ( false ) ;
2083+
2084+ // edge case: this is a not parseable token that is still accepted by the server
2085+ repository . Server . Authentication . AccessToken = "not parseable token" ;
2086+
2087+ // ACT
2088+ var content = await repository . GetCurrentUserAsync ( CancellationToken . None )
2089+ . ConfigureAwait ( false ) ;
2090+
2091+ // ASSERT
2092+ var requestedUri = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 1 ] . GetArguments ( ) . First ( ) ! ;
2093+ Assert . IsNotNull ( requestedUri ) ;
2094+ Assert . AreEqual ( "/OData.svc/('Root')/GetCurrentUser?metadata=no" , requestedUri . PathAndQuery ) ;
2095+
2096+ Assert . IsNotNull ( content ) ;
2097+ Assert . AreEqual ( "Admin" , content . Name ) ;
2098+ }
2099+ [ TestMethod ]
2100+ public async Task Repository_Auth_GetCurrentUser_ValidUser_ApiKey ( )
2101+ {
2102+ // ALIGN
2103+ var restCaller = CreateRestCallerFor ( @"{""Name"": ""Admin"", ""Id"": 1, ""Type"": ""User"" }" ) ;
2104+ var repositories = GetRepositoryCollection ( services =>
2105+ {
2106+ services . AddSingleton ( restCaller ) ;
2107+ } ) ;
2108+ var repository = await repositories . GetRepositoryAsync ( "local" , CancellationToken . None )
2109+ . ConfigureAwait ( false ) ;
2110+
2111+ // we provide an api key instead of an access token
2112+ repository . Server . Authentication . ApiKey = "valid api key" ;
2113+
2114+ // ACT
2115+ var content = await repository . GetCurrentUserAsync ( CancellationToken . None )
2116+ . ConfigureAwait ( false ) ;
2117+
2118+ // ASSERT
2119+ var requestedUri = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 1 ] . GetArguments ( ) . First ( ) ! ;
2120+ Assert . IsNotNull ( requestedUri ) ;
2121+ Assert . AreEqual ( "/OData.svc/('Root')/GetCurrentUser?metadata=no" , requestedUri . PathAndQuery ) ;
2122+
2123+ Assert . IsNotNull ( content ) ;
2124+ Assert . AreEqual ( "Admin" , content . Name ) ;
2125+ }
2126+ [ TestMethod ]
2127+ public async Task Repository_Auth_GetCurrentUser_Visitor_NoToken ( )
2128+ {
2129+ // ALIGN
2130+ var restCaller = CreateRestCallerFor ( @"{ ""d"": {
2131+ ""Name"": ""Visitor"", ""Id"": 6, ""Type"": ""User"" }}" ) ;
2132+ var repositories = GetRepositoryCollection ( services =>
2133+ {
2134+ services . AddSingleton ( restCaller ) ;
2135+ } ) ;
2136+ var repository = await repositories . GetRepositoryAsync ( "local" , CancellationToken . None )
2137+ . ConfigureAwait ( false ) ;
2138+
2139+ // no token
2140+ repository . Server . Authentication . AccessToken = null ;
2141+
2142+ // ACT
2143+ var content = await repository . GetCurrentUserAsync ( CancellationToken . None )
2144+ . ConfigureAwait ( false ) ;
2145+
2146+ // ASSERT
2147+ var requestedUri = ( Uri ) restCaller . ReceivedCalls ( ) . ToArray ( ) [ 1 ] . GetArguments ( ) . First ( ) ! ;
2148+ Assert . IsNotNull ( requestedUri ) ;
2149+ Assert . AreEqual ( "/OData.svc/Root/IMS/BuiltIn/Portal('Visitor')?metadata=no" , requestedUri . PathAndQuery ) ;
2150+
2151+ Assert . IsNotNull ( content ) ;
2152+ Assert . AreEqual ( "Visitor" , content . Name ) ;
2153+ }
2154+
19592155 /* ====================================================================== CUSTOM REQUESTS */
19602156
19612157 private class CustomNestedObject
0 commit comments