New Posts New Posts RSS Feed: CompositionContext.Fake and Login
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

CompositionContext.Fake and Login

 Post Reply Post Reply
Author
robert.hui View Drop Down
Newbie
Newbie


Joined: 11-Aug-2010
Posts: 5
Post Options Post Options   Quote robert.hui Quote  Post ReplyReply Direct Link To This Post Topic: CompositionContext.Fake and Login
    Posted: 25-Mar-2011 at 3:39pm
I have a question about how to use the CompositionContext.Fake. I read over the DRC documentation on faking: http://drc.ideablade.com/xwiki/bin/view/Documentation/discovery-compositioncontext-fake, but it only covered a simple case where no Login credentials were required.

I'm trying to set up a fake context for use in unit tests, by inflating an in-memory database to run some of our queries. I'm running into a chicken and egg problem with our login process, though.

By default, the first login to the BOS is a LoginCredential. This allows the user to read back a list of users (in our UI, this let's us show a dropdown). All other access is locked down until a user logs in with credentials. My question is, how do I inject fake user data into the system *before* log in, so that I can authenticate against credentials in the DB?

Is this even possible (via a RestoreCacheState, for example)?

Thanks,
Rob
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 28-Mar-2011 at 1:39pm
Hi robert.hui;

If I understand your question correctly, I believe you can accomplish what you want with IEntityServerFakeBackingStore.RestoreAsync operation.

The idea is to populate a real EntityManager's cache with some Users data and allow a fake EntityManager to import that cache to its own cache thus populating the fake EntityManager with some data.

Please see code snippets below.

    public void PopulateFakeEmWithUsersBeforeLogin() {
      var fakeEm = new NorthwindIBEntityManager(compositionContextName: CompositionContext.Fake.Name); 
      var coop = Coroutine.Start( () => PopulateWithUsersCore(fakeEm) );
      coop.Completed += (s, args) => {
        var isSuccessful = args.CompletedSuccessfully;
        var error = args.Error;
        fakeEm.Users.ExecuteAsync(op => {
          var isSuccessful2 = op.CompletedSuccessfully;
          var error2 = op.Error;
          var list = new List<User>(op.Results);
        });
      };
    }

    private IEnumerable<INotifyCompleted> PopulateWithUsersCore(NorthwindIBEntityManager fakeEm) {
      var realEm = new NorthwindIBEntityManager();
      var fakeBackingStore = fakeEm.CompositionContext.GetFakeBackingStore();

      //Populate realEm cache with Users
      var query = realEm.Users;
      var queryOp = query.ExecuteAsync();
      queryOp.Completed += (s, args) => {
        var isSuccessful = args.CompletedSuccessfully;
        var error = args.Error;
        var users = new List<User>(args.Results);
      };
      yield return queryOp;

      //Populate fakeEm with realEM cache
      var realEmCacheState = realEm.CacheStateManager.GetCacheState();
      var restoreOp = fakeBackingStore.RestoreAsync(realEmCacheState);
      restoreOp.Completed += (s, args) => {
        var isSuccessful = args.CompletedSuccessfully;
        var error = args.Error;
      };
      yield return restoreOp;
    }


Edited by DenisK - 28-Mar-2011 at 1:55pm
Back to Top
robert.hui View Drop Down
Newbie
Newbie


Joined: 11-Aug-2010
Posts: 5
Post Options Post Options   Quote robert.hui Quote  Post ReplyReply Direct Link To This Post Posted: 06-Apr-2011 at 2:37pm
Dennis,

Thanks for the help on that. Something very similar to that appears to have worked on my end.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down